实体将两次添加到数据库

时间:2016-03-21 02:34:56

标签: c# entity-framework entity-framework-6

问题(参见下面代码中的AbandonOrderTest3):

新的客户对象和订单对象。

客户被设置为订单的属性。

订单附加到上下文但是不调用SaveChanges。

上下文已被处理。

再次新建订单对象,并将客户对象设置为属性。

创建新上下文并添加订单。调用SaveChanges。

订单被添加两次到数据库(这是问题,应该添加一次)。

.Net版本:4.5,EF版本:6.1.3

    public void AbandonOrderTest1()
    {
        // Works as expected
        Customer c1 = new Customer { Name = "C1", ID = 9 };  // Customer with this ID already exists
        Order order = new Order { Customer = c1, CustomerID = c1.ID };

        using (Repository.db db = new Repository.db(_connectionString))
        {
            db.Orders.Add(order);
            db.Entry(order.Customer).State = EntityState.Detached;
            db.SaveChanges();
        }
    }

    public void AbandonOrderTest2()
    {
        // Order is added once, customer is added again as expected
        Customer c1 = new Customer { Name = "C1", ID = 9 };  // Customer with this ID already exists
        Order order = new Order { Customer = c1, CustomerID = c1.ID };

        using (Repository.db db = new Repository.db(_connectionString))
        {
            db.Orders.Add(order);
            // Save changes is not called here
        }

        // db is disposed at this point.  Neither order nor c1 should be 
        // attached to any context as no context exists for them to be 
        // attached to.


        using (Repository.db db = new Repository.db(_connectionString))
        {
            db.Orders.Add(order);
            db.SaveChanges();
        }
    }

    public void AbandonOrderTest3()
    {
        // Order is added twice, customer is added again as expected
        Customer c1 = new Customer { Name = "C1", ID = 9 };  // Customer with this ID already exists
        Order order = new Order { Customer = c1, CustomerID = c1.ID };

        using (Repository.db db = new Repository.db(_connectionString))
        {
            db.Orders.Add(order);
            // Save changes is not called here
        }

        // db is disposed at this point.  Neither order nor c1 should be 
        // attached to any context as no context exists for them to be 
        // attached to.

        // --- New up the order again 
        order = new Order { Customer = c1, CustomerID = c1.ID };  

        using (Repository.db db = new Repository.db(_connectionString))
        {
            var orderState = db.Entry(order).State;         // Detached
            var custState = db.Entry(order.Customer).State; // Detached
            db.Orders.Add(order);
            db.SaveChanges();
        }
    }

2 个答案:

答案 0 :(得分:0)

我写了一篇关于这个问题的文章:Entity Framework - Entity Added twice in Database

事实上,订单没有添加两次。创建的第一个订单已添加,然后创建的第二个订单已添加到数据库中。

var customer = new Association_OneToMany_Left();
var order1 = new Association_OneToMany_Right();
var order2 = new Association_OneToMany_Right();

order1.Left = customer;
order2.Left = customer;

using (var ctx = new TestContext())
{
    // Adding the first order create a new order list in the customer entity
    // customer.Right (The order list) contains now one element, the order1
    ctx.Association_OneToMany_Rights.Add(order1);

    // DO NOT save in the database.
}

using (var ctx = new TestContext())
{
    // customer.Right contains now two elements (order1 previously added and order2)
    ctx.Association_OneToMany_Rights.Add(order2);

    // Save left, order1 added from previous context and order2 added in this context
    ctx.SaveChanges();
}

答案 1 :(得分:-1)

using (Repository.db db = new Repository.db(_connectionString))
        {
            db.Orders.Add(order);
            // Save changes is not called here
        }

你必须添加..

using (Repository.db db = new Repository.db(_connectionString))
            {
                db.Orders.Add(order);
                <php? cout << db.Orders.Add ?!>

            }