C# 首先在实体框架代码中映射多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19646337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping many to many relationship in entity framework code first
提问by Lai32290
I'm try make a test in EF, creating a many to many relationship, because I always mapping One to One or One to Many, I has get a example in the internet for try, the example is working for insert registers, but I can't read registers
我尝试在 EF 中进行测试,创建多对多关系,因为我总是映射一对一或一对多,我在互联网上有一个示例可供尝试,该示例适用于插入寄存器,但是我无法读取寄存器
Here is my classes, I don't know what is HashSet
, I get this code in the site
这是我的课程,我不知道是什么HashSet
,我在网站上得到了这个代码
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Course> CoursesAttending { get; set; }
public Person()
{
CoursesAttending = new HashSet<Course>();
}
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public ICollection<Person> Students { get; set; }
public Course()
{
Students = new HashSet<Person>();
}
}
Here is my Context
这是我的上下文
public class SchoolContext : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Person> People { get; set; }
public SchoolContext()
: base("MyDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>().
HasMany(c => c.Students).
WithMany(p => p.CoursesAttending).
Map(
m =>
{
m.MapLeftKey("CourseId");
m.MapRightKey("PersonId");
m.ToTable("PersonCourses");
});
}
}
When I insert registers is right
当我插入寄存器是正确的
static void Main(string[] args)
{
using (SchoolContext db = new SchoolContext())
{
Course math = new Course();
Course history = new Course();
Course biology = new Course();
math.Title = "Math";
history.Title = "History";
biology.Title = "Biology";
db.Courses.Add(math);
db.Courses.Add(history);
db.Courses.Add(biology);
Person john = new Person();
john.FirstName = "John";
john.LastName = "Paul";
john.CoursesAttending.Add(history);
john.CoursesAttending.Add(biology);
db.People.Add(john);
db.SaveChanges();
}
}
But when I try select register for show content, is not work, it just print nothing
但是当我尝试选择注册显示内容时,不起作用,它只是不打印任何内容
static void Main(string[] args)
{
using (SchoolContext db = new SchoolContext())
{
Pearson p = db.Peasons.First();
Console.WriteLine(p.CoursesAttending.First().Title);
}
}
I had check in the database, registers exist, what is the problem?
我在数据库中检查过,寄存器存在,有什么问题?
Please teach me how to make select in many to many relationship with code first.
请教我如何在多对多关系中使用代码进行选择。
采纳答案by Gert Arnold
First off, you may want to enable lazy loading by making the collections virtual:
首先,您可能希望通过将集合设置为虚拟来启用延迟加载:
public virtual ICollection<Course> CoursesAttending { get; set; }
public virtual ICollection<Person> Students { get; set; }
This allows EF to create derived classes (proxies) from Course
and Person
that override the collections with logic to load their data from the data store.
这允许 EF 创建派生类(代理),Course
并Person
使用逻辑覆盖集合以从数据存储加载它们的数据。
When you do that you will see that
当你这样做时,你会看到
Console.WriteLine(p.CoursesAttending.First().Title);
will execute a separate query to populate CoursesAttending
.
将执行一个单独的查询来填充CoursesAttending
.
Alternatively, or additionally, you may decide to prevent round trips to the database by eager loadinglike so:
或者,或者另外,您可以决定通过预先加载来阻止到数据库的往返,如下所示:
Person p = db.Persons.Include(p => p.CoursesAttending).First();
Which loads the Person
andthe CoursesAttending
in one shot.
它装载Person
和的CoursesAttending
一个镜头。
回答by Chris Wu
Remove your class constructor and that OnModelCreating method. EF handles Many to Many relationship. You don't need to do anything extra.
删除您的类构造函数和 OnModelCreating 方法。EF 处理多对多关系。你不需要做任何额外的事情。