【问题标题】:Entity Framework also Linq advice needed实体框架还需要 Linq 建议
【发布时间】:2011-09-30 20:25:27
【问题描述】:

我已经上了我的 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 查询

【问题讨论】:

  • 附带提示:如果您不想只获得电影中的角色,则需要更改架构。因为不是每个演员都出演过他们合作过的所有电影。 (就像梅尔吉布森没有出演《启示录》,但他导演了它......)

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


【解决方案1】:

您需要在模型中引入一个额外的实体WorkerMovie,并将WorkerMovie 之间的多对多关系转换为两个一对多关系——一个在Worker 和@987654326 之间@ 和 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2017-07-07
    • 2010-10-12
    • 2023-03-25
    • 2018-11-30
    相关资源
    最近更新 更多