实体框架:在WPF中创建和更新相关对象 [英] Entity Framework: Create and update related objects in WPF
问题描述
我通过实体框架创建和更新数据库中的数据有问题。我不知道在存储学生之前是否需要使
db.Groups.Attach(student.Group)
。没有这个保存学生后,我也有新的组与相同的名称,但其他GroupId。 此外,我无法更新学生,因为我得到异常:两个对象之间的关系无法定义,因为它们附加到不同的ObjectContext对象。
public class Student {
[Key]
public int StudentId {get; set;}
public string Name {get; set;}
public Group Group {get; set;}
}
public class Group {
[Key]
public int GroupId {get; set;}
public string Name {get; set;}
public virtual ICollection< Student>学生{get; set;}
}
。
public class StudentDao {
public void createStudent(Student student){
using(var db = new StorageContext()){
// without这也创造了新的集团。
db.Groups.Attach(student.Group);
db.Students.Add(student);
db.SaveChanges();
}
}
public void updateStudent(Student student){
using(var db = new StorageContext()){
var original = db。 Students.Find(student.StudentId);
if(original!= null){
original.Name = student.Name;
original.Group = student.Group;
db.SaveChanges(); // exception
}
}
}
}
没有这个( db.Groups.Attach(student.Group))保存后学生我也有新的组
这是因为添加
将实体添加到 DbSet
标记所有附加实体,上下文尚未跟踪。这是一个EF功能,喜欢与否,所以你必须首先附加你不想重新插入的实体。
此外,我无法更新学生,因为我得到例外
由于某些原因,在更新方法中,学生及其组仍然依附于上下文。显然,在 StudentDao
类中还有一些其他上下文活动。你必须确保这个上下文的生命周期结束,当你更新学生或其他(最好的)分离学生和组从它。
一个非主题的建议:如果可以,放弃这个DAO模式。当您在处理一个工作单位的类似服务的方法中使用 DbContext
及其 DbSet
时,EF工作得更好。有了这些DAO,就不可能在事务上进行工作,并导致一堆重复的代码。
I'm using EF 6.0 and code-first approach.
I have problem with create and update data in db via Entity Framework. I'm not sure if I need to make db.Groups.Attach(student.Group)
before storing Student. Without this after saving Student I also have new Group with the same Name but other GroupId.
Moreover I can't update student because I'm getting exception: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
public class Student {
[Key]
public int StudentId {get; set;}
public string Name {get; set;}
public Group Group {get; set;}
}
public class Group {
[Key]
public int GroupId{ get; set;}
public string Name {get; set;}
public virtual ICollection<Student> Students {get; set;}
}
.
public class StudentDao {
public void createStudent(Student student) {
using (var db = new StorageContext()) {
// without this also creates new Group.
db.Groups.Attach(student.Group);
db.Students.Add(student);
db.SaveChanges();
}
}
public void updateStudent(Student student) {
using (var db = new StorageContext()) {
var original = db.Students.Find(student.StudentId);
if (original != null) {
original.Name = student.Name;
original.Group = student.Group;
db.SaveChanges(); //exception
}
}
}
}
Without this (db.Groups.Attach(student.Group)) after saving Student I also have new Group
That's because Add
ing an entity to a DbSet
marks all adhering entities that are not yet tracked by the context as Added. This is an EF feature, like it or not, so you have to first attach the entities you don't want to re-insert.
Moreover I can't update student because I'm getting exception
For some reason, in the update method the student and its group are still attached to a context. Apparently, there is some other context active in the StudentDao
class. You have to make sure this context's lifespan is over when you update the student or else (second best) detach the student and the group from it.
An off-topic advice: if you can, abandon this DAO pattern. EF works much better when you use the DbContext
and its DbSet
s in service-like methods that handle one unit of work. With these DAO's it's impossible to work transactionally and they cause piles of repeated code.
这篇关于实体框架:在WPF中创建和更新相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!