插入/更新多个实体框架。我该怎么做? [英] Insert/Update Many to Many Entity Framework . How do I do it?
问题描述
假设我有3个表
- 类:ClassID-ClassName
- 学生:StudentID-FirstName-Surname
- StudentClass:StudentID-ClassID
添加所有关系并通过模型浏览器更新模型后,我注意到StudentClass没有出现,这似乎是默认行为。 >
现在我需要同时执行插入和更新。你怎么做呢?任何代码示例或链接,我可以下载一个例子,或者你可以休息5分钟?
对象),您有一个类
对象,其中包含学生
和学生
对象,其集合为类
。由于您的 StudentClass
表只包含Ids,没有额外的信息,EF不会为连接表生成一个实体。这是正确的行为,这就是你的期望。
现在,当插入或更新时,尝试考虑对象。例如。如果要插入一个有两个学生的课程,请创建 Class
对象, Student
对象,将学生添加到类学生
集合将类
对象添加到上下文中,并调用 SaveChanges
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $新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
表中的两个条目链接在一起。
您基本上做同样的更新。只需获取数据,通过添加和删除集合中的对象来修改图形,调用 SaveChanges
。有关详细信息,请查看此类似问题。
修改:
根据您的评论,您需要插入一个新的 Class
并添加两个现有的学生
:
使用(var context = new YourContext())
{
var mathClass = new Class {Name =Math};
学生student1 = context.Students.FirstOrDefault(s => s.Name ==Alice);
学生student2 = context.Students.FirstOrDefault(s => s.Name ==Bob);
mathClass.Students.Add(student1);
mathClass.Students.Add(student2);
context.AddToClasses(mathClass);
context.SaveChanges();
}
既然这两个学生都已经在数据库中,但是由于它们现在位于学生
收集 Class
中,所以两个条目将被插入 StudentClass
表。
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.
Suppose I have 3 tables
- Class : ClassID-ClassName
- Student : StudentID-FirstName-Surname
- StudentClass : StudentID-ClassID
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.
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?
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.
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();
}
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.
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.
Edit:
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();
}
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屋!