prevent实体框架插入航行属性值 [英] Prevent Entity Framework to Insert Values for Navigational Properties

查看:107
本文介绍了prevent实体框架插入航行属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架4.0的WPF应用程序。当我试图保存对象,我有一个主键异常,但主键是自动递增的字段,我无法理解的异常的原因。

I am working on a WPF application using Entity Framework 4.0. When I tried to save the object, I got a primary key exception, but the primary key is an AutoIncremented field and I cannot understand the reason for the exception.

所以尝试这做那,和一点点调试和使用SQL事件探查器后,我发现插入我的对象之前,记录必须在父表中插入,因为我设置对象的导航属性。

So after trying this and that, and a little debugging and using the SQL profiler, I found out that prior to inserting my object, a record must be inserted in the parent table, as I set the navigation property of that object.

所以,关键是,如果要插入Employee对象并设置其部门认定Employee.Department = deptObject一种尝试,然后一个新的记录被设置为部门物体插入。

So the crux is if an attempt to insert Employee object and set its department as Employee.Department = deptObject, then a new record is set to be inserted on department object.

请建议我好歹由导航属性的对象不会在数据库中插入任何财产或任何方法,任何事情。

Kindly suggest me someway by which navigational property objects won't be inserted in the database, any property or any method, Anything.

感谢

推荐答案

这是这样的EF是如何工作的,如果你错误地使用分离的实体。我想你使用的是这样的:

This is the way how EF works if you incorrectly use detached entities. I suppose you are using something like this:

var employee = new Employee();
employee.Department = GetDepartmentFromSomewhere(departmentId);

...

using (var context = new YourContext())
{
    context.Employees.AddObject(employee);
    context.SaveChanges();
}

这code prepared雇员实体中,添加参照现有的部门,并保存新员工到数据库中。哪里有问题?问题是, ADDOBJECT 不添加唯一的员工,但整个对象图。这是EF如何工作的 - 你不能在这里对象的一部分连接到环境和不属于对象图。 ADDOBJECT 在图中作为一个新的(新=插入数据库)增加了每个对象。所以你必须你的操作要么改变序列或手动固定实体的状态,这样你的情况下知道部门已经存在。

This code prepared employee entity, added reference to existing department and saved new employee to the database. Where is the problem? The problem is that AddObject doesn't add only employee but whole object graph. That is how EF works - you cannot have object graph where part of objects are connected to context and part of not. AddObject adds every object in the graph as a new one (new one = insert in database). So you must either change sequence of your operations or fix state of entities manually so that your context knows that department already exists.

首先解决方案 - 用于载入部门同样的情况下,节省员工:

First solution - use the same context for loading department and saving employee:

using (var context = new YourContext())
{
    var employee = new Employee();
    ...
    context.Employees.AddObject(employee);

    employee.Department = context.Departments.Single(d => d.Id == departmentId);
    context.SaveChanges();
}

第二类解决方案 - 实体单独连接上下文后,使实体之间的参考:

Second solution - connect entities to the context separately and after that make reference between entities:

var employee = new Employee();
...

var department = GetDepartmentFromSomewhere(departmentId);

using (var context = new YourContext())
{
    context.Employees.AddObject(employee);
    context.Departments.Attach(department);
    employee.Department = department;

    context.SaveChanges();
}

第三个解决方案 - 该部门的正确状态手动使上下文不重新插入:

Third solution - correct state of the department manually so that context doesn't insert it again:

var employee = new Employee();
employee.Department = GetDepartmentFromSomewhere(departmentId);

...

using (var context = new YourContext())
{
    context.Employees.AddObject(employee);
    context.ObjectStateManager.ChangeObjectState(employee.Department, 
                                                 EntityState.Unchanged);
    context.SaveChanges();
}

这篇关于prevent实体框架插入航行属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆