简体   繁体   English

实体框架:在WPF中创建和更新相关对象

[英]Entity Framework: Create and update related objects in WPF

I'm using EF 6.0 and code-first approach. 我正在使用EF 6.0和代码优先方法。 I have problem with create and update data in db via Entity Framework. 我有通过Entity Framework在db中创建和更新数据的问题。 I'm not sure if I need to make db.Groups.Attach(student.Group) before storing Student. 我不确定在存储Student之前是否需要创建db.Groups.Attach(student.Group) Without this after saving Student I also have new Group with the same Name but other GroupId. 保存学生后没有这个,我也有新的组,其名称与其他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. 此外,我无法更新学生,因为我遇到异常: 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 没有这个( db.Groups.Attach(student.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. 这是因为Add实体AddDbSet 所有尚未被上下文跟踪的粘附实体标记为已添加。 This is an EF feature, like it or not, so you have to first attach the entities you don't want to re-insert. 这是一个EF功能,不管你喜不喜欢,所以你必须先附加你不想重新插入的实体。

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. 显然, StudentDao课程中还有其他一些活动。 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. 一个偏离主题的建议:如果可以的话,放弃这个DAO模式。 EF works much better when you use the DbContext and its DbSet s in service-like methods that handle one unit of work. 当您在处理一个工作单元的类似服务的方法中使用DbContext及其DbSet时,EF工作得更好。 With these DAO's it's impossible to work transactionally and they cause piles of repeated code. 使用这些DAO,它不可能在事务上工作,并且它们会导致大量重复的代码。

Been a while since I worked on Entity but from what I remember you can't just change the Group, you have to give it a GroupID field as well, change that instead and then reload/update the Student object from the database so that the Group object gets loaded and assigned from within the same context. 自从我在Entity工作了一段时间后,但是从我记忆中你不能只改变组,你必须给它一个GroupID字段,改为改变它然后从数据库重新加载/更新Student对象,以便组对象从同一上下文中加载和分配。

This is just one of the reasons I use NHibernate. 这只是我使用NHibernate的原因之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM