实体框架也需要Linq建议

时间:2011-09-30 20:25:27

标签: linq entity-framework-4.1 ef-code-first

我上了DB 3桌子 moviesworkersworkermovies(这是关系表)

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 查询

1 个答案:

答案 0 :(得分: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