如何拥有多对多的关联实体框架code首先 [英] How to have Many to Many Association in Entity Framework Code First

查看:125
本文介绍了如何拥有多对多的关联实体框架code首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用EF,我看了一些伟大的教程视频。我坚持以下几点。

I am just getting started with EF and I watched some great tutorial videos. 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>();}
}

现在,当我生成我的文件表中有一个是PersonID和数据库事件ID。

Now when I generate the database my file table has a PersonID AND EventID.

我希望能够让用户将文件附加到人和/或事件。

I want to be able to let users attach files to people and/or events.

推荐答案

什么你正在为EF code首先在映射1对多关联数据库方面的默认行为。
例如你有一个的ICollection&LT;文件&gt; 的Person类,因此,EF将创建文件表FK(PERSONID)并将其映射到编号PK的人表。
搜索结果
现在,我的猜测是,你想有一个的多对多的文件和人之间的关系,使每个文件可以涉及到很多人,每个人都可以有很多的文件(Event对象作为同样的故事好)。实现这一目标的方法之一是把文件类指向事件和Person类的导航性能。所以,你的模式应该改变这样:

What you are getting is the default behavior of EF Code First in terms of mapping a 1 to Many association to the database. 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.

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). One way to achieve this is to put navigation properties on File class pointing to Event and Person classes. 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; }
}

其结果是,EF将创建链接表(Events_Files和Files_Persons)这些多对多关联映射到数据库。

As a result, EF will create link tables (Events_Files and Files_Persons) to map these many to many associations to the database.

更新:
结果
当使用波苏斯与EF,如果您标记您的导航属性的虚拟的你会选择加入一些额外的EF的支持喜欢的延迟加载的和的关系Fixup时的。所以一般在被认为是一个很好的做法导航属性的虚拟关键字。

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. So in general have a virtual keyword in navigation properties considered to be a good practice.

这篇关于如何拥有多对多的关联实体框架code首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆