实体框架POCO - 更新集合

3
我有一个简单的模型,其中包含名为Customer和Address的类,如下所示:
public class Customer : BusinessEntity
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime? DateOfBirth { get; set; }

    public decimal? CreditLimit { get; set; }

    public virtual List<Address> Addresses { get; set; }

    public string Email { get; set; }
}

public class Address : BusinessEntity
{
    public string Street { get; set; }

    public string Floor { get; set; }

    public Customer Customer { get; set; }
}

我编写了一个单元测试,加载一个带有现有地址的客户,修改该地址并调用更新。以下是代码:

        Customer newCustomer = new Customer();

        newCustomer.FirstName = "Cosme";

        newCustomer.LastName = "Fulanito";

        newCustomer.Email = "anemail@mail.com";

        customerPersistence.Save(newCustomer);

        Customer existingCustomer = customerPersistence.FindByID(newCustomer.ID.Value);

        Assert.IsNotNull(existingCustomer, "Customer not found after saving");

        existingCustomer.LastName = "Fulanito Modified";

        existingCustomer.Addresses = new List<Address>();

        existingCustomer.Addresses.Add(new Address { Customer = existingCustomer, Floor = "21", Street = "Peron" });

        customerPersistence.Update(existingCustomer);

        Customer loadedCustomer = customerPersistence.FindByID(newCustomer.ID.Value);

        Assert.IsNotNull(loadedCustomer, "Customer not found after updating");

        Assert.IsTrue(loadedCustomer.LastName == existingCustomer.LastName, "Last name not updated");

        Assert.IsNotNull(loadedCustomer.Addresses, "Addresses collection is null");

        Assert.IsTrue(loadedCustomer.Addresses.Count > 0, "Addresses collection is empty");

        existingCustomer = customerPersistence.FindByID(newCustomer.ID.Value);

        Assert.IsNotNull(existingCustomer, "Customer not found after updating");

        existingCustomer.Addresses[0].Floor = "40";

        customerPersistence.Update(existingCustomer);

        Assert.IsTrue(loadedCustomer.Addresses[0].Floor == "40", "Address data not modified");

上下文是指继承自DBContext类的类的引用。我得到了这个错误:

'Address_Customer' AssociationSet 中的关系处于 'Deleted' 状态。鉴于多重性约束,相应的 'Address_Customer_Source' 也必须处于 'Deleted' 状态。

我错过了什么吗?我在 OnModelCreating 方法中定义 Customer 和 Address 之间的关系时有问题吗?我正在做这个:

modelBuilder.Entity<Address>()
            .HasRequired(p => p.Customer)
            .WithMany(p => p.Addresses)
            .Map(x => x.MapKey("CustomerID"))

感谢您,Gonzalo。

@LadislavMrnka 我从数据库中获取客户信息,然后将existingCustomer.Addresses[0].Floor设置为"40"。 - Gonzalo
将您的测试添加到问题中。 - Ladislav Mrnka
哪一行代码产生了这个错误? - Tommy Grovnes
@TommyGrovnes 当我调用SaveChanges时,会抛出异常。我注意到的一件事是,当我将客户实体的状态更改为“已修改”时,地址集合被清空了。 - Gonzalo
我猜测customerPersistence的Update方法是这样的:打开会话,执行更新操作,然后关闭会话,我的理解正确吗? - Rafal
请查看此答案:https://dev59.com/d1nUa4cB1Zd3GeqPdLrX - radu florescu
1个回答

1
请将Addresses属性更改为ICollection。然后,在更新详细信息时,请检查实体状态是否已分离。如果它已分离,则必须将更新的对象(客户以及地址)附加到上下文中,并告诉ObjectStateManager对象已被修改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接