2011-09-30 48 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