插入/更新多对多实体框架.我该怎么做? [英] Insert/Update Many to Many Entity Framework . How do I do it?
问题描述
我正在使用 EF4 并且是新手.我的项目中有多对多,似乎无法弄清楚如何插入或更新.我已经构建了一个小项目,只是为了看看它应该如何编码.
I'm using EF4 and new to it. I have a many to many in my project and cannot seem to work out how to insert or update. I have build a small project just to see how it should be coded.
假设我有 3 张桌子
- 类:类ID-类名
- 学生:StudentID-FirstName-Surname
- StudentClass:StudentID-ClassID
在添加所有关系并通过模型浏览器更新模型后,我注意到 StudentClass 没有出现,这似乎是默认行为.
After adding all the relationship and updated the model via model browser I have noticed that StudentClass does not appear, this seems to be default behaviour.
现在我需要同时进行插入和更新.你怎么做呢?任何代码示例或链接,我可以在其中下载示例,或者您能抽出 5 分钟吗?
Now I need to do both an Insert and Update. How do you do it? Any code samples or link where I can download an example, or can you spare 5 mins?
推荐答案
就实体(或对象)而言,您有一个 Class
对象,其中包含 Students
的集合以及具有 Classes
集合的 Student
对象.由于您的 StudentClass
表仅包含 Id 而没有额外信息,因此 EF 不会为连接表生成实体.这是正确的行为,也是您所期望的.
In terms of entities (or objects) you have a Class
object which has a collection of Students
and a Student
object that has a collection of Classes
. Since your StudentClass
table only contains the Ids and no extra information, EF does not generate an entity for the joining table. That is the correct behaviour and that's what you expect.
现在,在进行插入或更新时,请尝试从对象的角度进行思考.例如.如果要插入一个有两个学生的班级,创建Class
对象,Student
对象,将学生添加到班级Students
集合中添加Class
对象到上下文并调用 SaveChanges
:
Now, when doing inserts or updates, try to think in terms of objects. E.g. if you want to insert a class with two students, create the Class
object, the Student
objects, add the students to the class Students
collection add the Class
object to the context and call SaveChanges
:
using (var context = new YourContext())
{
var mathClass = new Class { Name = "Math" };
mathClass.Students.Add(new Student { Name = "Alice" });
mathClass.Students.Add(new Student { Name = "Bob" });
context.AddToClasses(mathClass);
context.SaveChanges();
}
这将在 Class
表中创建一个条目,Student
表中的两个条目和 StudentClass
表中的两个条目将它们链接在一起.
This will create an entry in the Class
table, two entries in the Student
table and two entries in the StudentClass
table linking them together.
您基本上对更新执行相同的操作.只需获取数据,通过从集合中添加和删除对象来修改图形,调用 SaveChanges
.查看这个类似的问题了解详情.
You basically do the same for updates. Just fetch the data, modify the graph by adding and removing objects from collections, call SaveChanges
. Check this similar question for details.
编辑:
根据你的评论,你需要插入一个新的Class
并添加两个现有的Students
:
According to your comment, you need to insert a new Class
and add two existing Students
to it:
using (var context = new YourContext())
{
var mathClass= new Class { Name = "Math" };
Student student1 = context.Students.FirstOrDefault(s => s.Name == "Alice");
Student student2 = context.Students.FirstOrDefault(s => s.Name == "Bob");
mathClass.Students.Add(student1);
mathClass.Students.Add(student2);
context.AddToClasses(mathClass);
context.SaveChanges();
}
因为两个学生都已经在数据库中了,所以不会被插入,但是因为他们现在在Class
的Students
集合中,所以会有两个条目插入到 StudentClass
表中.
Since both students are already in the database, they won't be inserted, but since they are now in the Students
collection of the Class
, two entries will be inserted into the StudentClass
table.
这篇关于插入/更新多对多实体框架.我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!