6

我正在尝试Customer使用 ASP.NET Web API 和 Entity Framework 5 代码优先更新我的数据库中的 a ,但它不起作用。我的实体如下所示:

public class CustomerModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    // More fields

    public ICollection<CustomerTypeModel> CustomerTypes { get; set; }
}

public class CustomerTypeModel
{
    public int Id { get; set; }
    public string Type { get; set; }

    [JsonIgnore]
    public ICollection<CustomerModel> Customers { get; set; }
}

没什么特别的。我已经构建了一个 Web 界面,用户可以在其中通过提供名称并检查一个或多个客户类型来添加客户。当点击提交按钮时,数据被发送到我的 Web API 方法:

public void Put([FromBody]CustomerModel customer)
{
    using (var context = new MyContext())
    {
        context.Customers.Attach(customer);
        context.Entry(customer).State = EntityState.Modified;
        context.SaveChanges();
    }
}

这会更新客户字段,但会忽略相关的客户类型。传入的customer对象确实包含一个CustomerTypes它应该关联的列表:

[0] => { Id: 1, Type: "Finance", Customers: Null },
[1] => { Id: 2, Type: "Insurance", Customers: Null }
[2] => { Id: 3, Type: "Electronics", Customers: Null }

但是,EF 不会查看此列表并添加/删除关联实体,而是忽略它。新的关联会被忽略,现有的关联即使应该被删除也会保留。

我在将客户插入数据库时​​遇到了类似的问题,当我将这些实体的状态调整为EntityState.Unchanged. 自然地,我尝试在我的更新场景中应用同样的魔法修复:

public void Put([FromBody]CustomerModel customer)
{
    using (var context = new MyContext())
    {
        foreach (var customertype in customer.CustomerTypes)
        {
            context.Entry(customertype).State = EntityState.Unchanged;
        }

        context.Customers.Attach(customer);
        context.Entry(customer).State = EntityState.Modified;
        context.SaveChanges();
    }
}

但 EF 一直显示相同的行为。

有想法该怎么解决这个吗?还是我真的应该手动清除列表CustomerTypes然后手动添加它们?

提前致谢。

J.P

4

2 回答 2

10

仅通过设置实体状态并不能真正解决这个问题。您必须首先从数据库中加载客户,包括其所有当前类型,然后根据已发布客户的更新类型集合从加载的客户中删除类型或添加类型。更改跟踪将完成其余工作以从连接表中删除条目或插入新条目:

public void Put([FromBody]CustomerModel customer)
{
    using (var context = new MyContext())
    {
        var customerInDb = context.Customers.Include(c => c.CustomerTypes)
            .Single(c => c.Id == customer.Id);

        // Updates the Name property
        context.Entry(customerInDb).CurrentValues.SetValues(customer);

        // Remove types
        foreach (var typeInDb in customerInDb.CustomerTypes.ToList())
            if (!customer.CustomerTypes.Any(t => t.Id == typeInDb.Id))
                customerInDb.CustomerTypes.Remove(typeInDb);

        // Add new types
        foreach (var type in customer.CustomerTypes)
            if (!customerInDb.CustomerTypes.Any(t => t.Id == type.Id))
            {
                context.CustomerTypes.Attach(type);
                customerInDb.CustomerTypes.Add(type);
            }

        context.SaveChanges();
    }
}
于 2013-05-16T18:03:09.253 回答
0

更清洁的解决方案是:

public void Put([FromBody]CustomerModel customer)
{
    using (var context = new MyContext())
    {
        var customerInDb = context.Customers.Include(c => c.CustomerTypes)
            .Single(c => c.Id == customer.Id);

        // Updates the Name property
        context.Entry(customerInDb).CurrentValues.SetValues(customer);

        // Remove types
        customer.CustomerTypes.Clear();

        // Add new types
        foreach (var type in customer.CustomerTypes) 
        {
            context.CustomerTypes.Attach(type);
            customerInDb.CustomerTypes.Add(type);
        }

        context.SaveChanges();
    }
}
于 2014-03-06T16:40:28.257 回答