简体   繁体   English

如何在实体框架代码中有多对多关联

[英]How to have Many to Many Association in Entity Framework Code First

I am just getting started with EF and I watched some great tutorial videos. 我刚刚开始使用EF,我观看了一些精彩的教程视频。 I am stuck with the following. 我坚持以下。

I have a class for a collection of files, I would like these to be tied to events and/or people 我有一个文件集合的类,我希望这些与事件和/或人联系在一起

public class file{
    public int id {get;set;}
    public string path {get;set;}
}

public event {
    public int id {get;set;}
    public string eventname {get;set}
    public virtual ICollection<file> files {get;set;}
    public event(){ files = new list<file>();}
}

public person {
    public int id {get;set;}
    public string name {get;set}
    public virtual ICollection<file> files {get;set;}
    public person(){ files = new list<file>();}
}

Now when I generate the database my file table has a PersonID AND EventID. 现在当我生成数据库时,我的文件表有一个PersonID AND EventID。

I want to be able to let users attach files to people and/or events. 我希望能够让用户将文件附加到人和/或事件。

What you are getting is the default behavior of EF Code First in terms of mapping a 1 to Many association to the database. 在将1到多个关联映射到数据库方面,您获得的是EF Code First的默认行为。 For example you have a ICollection<File> on Person class, as a result, EF will create a FK on Files table (PersonId) and map it to Id PK on Persons table. 例如,您在Person类上有一个ICollection<File> ,因此,EF将在Files表(PersonId)上创建一个FK并将其映射到Persons表上的Id PK。

Now, my guess is that you like to have a many to many relationship between File and Person, so that each file can relates to many Persons and each Person can have many files (same story for Event object as well). 现在,我的猜测是你喜欢在File和Person之间建立多对多关系,这样每个文件都可以与很多人相关,每个Person都可以有很多文件(同样的Event对象也是如此)。 One way to achieve this is to put navigation properties on File class pointing to Event and Person classes. 实现此目的的一种方法是将File类的导航属性指向Event和Person类。 So, your model should be changed to this: 所以,你的模型应该改为:

public class File {
    public int Id { get; set; }
    public string Path { get; set; }
    public virtual ICollection<Event> Events { get; set; }
    public virtual ICollection<Person> Persons { get; set; }
}

public class Event {
    public int Id { get; set; }
    public string EventName { get; set; }
    public virtual ICollection<File> Files {get;set;}
}

public class Person {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<File> Files { get; set; }
}

public class MyContext : DbContext {
    public DbSet<Person> Persons { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<File> Files { get; set; }
}

As a result, EF will create link tables (Events_Files and Files_Persons) to map these many to many associations to the database. 因此,EF将创建链接表(Events_Files和Files_Persons)以将这些多个关联映射到数据库。

Update: 更新:
When using POCOs with EF, if you mark your navigation properties as virtual you will opt-in to some of the additional EF supports like Lazy Loading and Relationship Fixup . 将POCO与EF一起使用时,如果将导航属性标记为虚拟 ,则会选择加入一些其他EF支持,如延迟加载关系修复 So in general have a virtual keyword in navigation properties considered to be a good practice. 因此通常在导航属性中有一个虚拟关键字被认为是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM