2011-09-30 52 views
0

我已经得到了我的DB 3个表 moviesworkersworkermovies(这是关系表)实体框架也LINQ的建议需要

public class Movie 
{ 
    public Movie() 
    { 
     Genres = new List<Genre>(); 
     Formats = new List<Format>(); 
     ProductionCompanies = new List<ProductionCompany>(); 
     Workers = new List<Worker>(); 
    } 

    public int Id { get; set; } 
    public string Title { get; set; } 
    public DateTime ReleaseDate { get; set; } 
    public string StoryLine { get; set; } 
    public int RunTime { get; set; } 

    [ForeignKey("MPAARateId")] 
    public MPAARate MPAARate { get; set; } 
    public int MPAARateId { get; set; } 

    public byte[] ImageData { get; set; } 
    public string ImageMimeType { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public string OfficialSite { get; set; } 
    public int Budget { get; set; } 
    public int StatusId { get; set; } 

    public virtual ICollection<Genre> Genres { get; set; } 
    public virtual ICollection<Format> Formats { get; set; } 
    public virtual ICollection<ProductionCompany> ProductionCompanies { get; set; } 
    public virtual ICollection<Worker> Workers { get; set; } 
} 


public class Worker 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string LastName { get; set; } 
    public DateTime Birthday { get; set; } 
    public string Biography { get; set; } 
    public string BornName { get; set; } 
    public double Height { get; set; } 
    public DateTime? Died { get; set; } 
    public byte[] ImageData { get; set; } 
    public string ImageMimeType { get; set; } 
    public bool IsActor { get; set; } 
    public bool IsDirector { get; set; } 
    public bool IsWriter { get; set; } 
    public bool IsProducer { get; set; } 
    public bool IsStar { get; set; } 

    public virtual ICollection<Movie> Movies { get; set; } 
} 
在这种关系中我得到了 movieIdworkerId 但我

如果该人的行为或书面或生产者等也得到了一些字段

我如何定义relation entity类如果需要
当我想得到强制性的牛逼的是,在电影担任我如何写这样的LINQ 查询PPL

+0

提示:如果您不想仅获取在电影中扮演的角色,则需要更改模式。因为不是每个演员都是演员,他们都参与了他们合作过的所有电影。 (就像梅尔吉布森没有出演Apocalypto一样,但是他指导了......) –

回答

0

你需要在你的模型WorkerMovie引入额外的实体和转换WorkerMovie之间的许多一对多的关系,为两个一对多种关系 - 一个在WorkerWorkerMovie之间,另一个在MovieWorkerMovie之间。草图:

public class WorkerMovie 
{ 
    [Key, Column(Order = 0)] 
    public int WorkerId { get; set; } 
    [Key, Column(Order = 1)] 
    public int MovieId { get; set; } 

    public Worker Worker { get; set; } 
    public Movie Movie { get; set; } 

    public bool WorkedAsActor { get; set; } 
    public bool WorkedAsWriter { get; set; } 
    public bool WorkedAsProducer { get; set; } 
    // etc. 
} 

public class Movie 
{ 
    // ... 
    public virtual ICollection<WorkerMovie> WorkerMovies { get; set; } 
    // remove ICollection<Worker> Workers 

} 

public class Worker 
{ 
    // ... 
    public virtual ICollection<WorkerMovie> WorkerMovies { get; set; } 
    // remove ICollection<Movie> Movies 
} 

如果你想才发现谁是在一个特定的电影有movieId演员的工作人员,你可以写:

var workersAsActors = context.WorkerMovies 
    .Where(wm => wm.MovieId == movieId && wm.WorkedAsActor) 
    .Select(wm => wm.Worker) 
    .ToList(); 

这里是另一个答案,有许多非常类似的问题可能的查询的更多示例:Create code first, many to many, with additional fields in association table