2

我用 Northwind 数据库做了一个小项目来说明这个问题。

这是控制器的操作:

[HttpPost]
public ActionResult Edit(Product productFromForm)
{
    try
    {
        context.Products.Attach(productFromForm);
        var fromBD = context.Categories.Find(productFromForm.Category.CategoryID);
        productFromForm.Category = fromBD;
        context.Entry(productFromForm).State = EntityState.Modified;
        context.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

context 在 Controller 的构造函数中被实例化为new DatabaseContext().

public class DatabaseContext:DbContext
{
    public DatabaseContext()
        : base("ApplicationServices") {
        base.Configuration.ProxyCreationEnabled = false;
        base.Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder){

        modelBuilder.Configurations.Add(new ProductConfiguration());
        modelBuilder.Configurations.Add(new CategoriesConfiguration());
    }

    private class ProductConfiguration : EntityTypeConfiguration<Product> {
        public ProductConfiguration() {
            ToTable("Products");
            HasKey(p => p.ProductID);
            HasOptional(p => p.Category).WithMany(x=>x.Products).Map(c => c.MapKey("CategoryID"));
            Property(p => p.UnitPrice).HasColumnType("Money");
        }
    }

    private class CategoriesConfiguration : EntityTypeConfiguration<Category> {
        public CategoriesConfiguration() {
            ToTable("Categories");
            HasKey(p => p.CategoryID);
        }
    }
}

public class Category {
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

public class Product {
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public string QuantityPerUnit { get; set; }
    public decimal UnitPrice { get; set; }
    public Int16 UnitsInStock { get; set; }
    public Int16 UnitsOnOrder { get; set; }
    public Int16 ReorderLevel { get; set; }
    public bool Discontinued { get; set; }
    public virtual Category Category { get; set; }
}

问题是我可以保存产品中的任何内容,但不能保存类别的更改。

对象 productFromForm 包含 productFromForm.Product.ProductID 内的新 CategoryID 没有问题。但是,当我Find()从上下文中检索对象的类别时,我有一个没有名称和描述的对象(都保持为 NULL)并且SaveChanges()即使属性的 ID 已更改,也不会修改引用Category

知道为什么吗?

4

2 回答 2

7

您(显然)更改的关系不会被保存,因为您并没有真正改变关系:

context.Products.Attach(productFromForm);

productFromForm此行将AND附加productFromForm.Category到上下文。

var fromBD = context.Categories.Find(productFromForm.Category.CategoryID);

此行返回附加对象productFromForm.Category,而不是数据库中的对象。

productFromForm.Category = fromBD;

这一行分配了同一个对象,所以它什么也不做。

context.Entry(productFromForm).State = EntityState.Modified;

此行仅影响 的标量属性productFromForm,而不影响任何导航属性。

更好的方法是:

// Get original product from DB including category
var fromBD = context.Products
    .Include(p => p.Category)  // necessary because you don't have a FK property
    .Single(p => p.ProductId == productFromForm.ProductId);

// Update scalar properties of product
context.Entry(fromBD).CurrentValues.SetValues(productFromForm);

// Update the Category reference if the CategoryID has been changed in the from
if (productFromForm.Category.CategoryID != fromBD.Category.CategoryID)
{
    context.Categories.Attach(productFromForm.Category);
    fromBD.Category = productFromForm.Category;
}

context.SaveChanges();

如果您将外键作为模型中的属性公开,它会变得容易得多 - 正如@Leniency 的回答和您上一个问题的回答中已经说过的那样。使用 FK 属性(并假设您Product.CategoryID直接绑定到视图而不是Product.Category.CategoryID),上面的代码简化为:

var fromBD = context.Products
    .Single(p => p.ProductId == productFromForm.ProductId);
context.Entry(fromBD).CurrentValues.SetValues(productFromForm);
context.SaveChanges();

或者,您可以Modified将使用 FK 属性的状态设置为:

context.Entry(productFromForm).State = EntityState.Modified;
context.SaveChanges();
于 2012-03-26T17:41:09.920 回答
2

问题在于 EF 跟踪关联更新的方式与值类型不同。当您这样做时context.Products.Attach(productFromForm);, productFromForm 只是一个不跟踪任何更改的 poco。当您将其标记为已修改时,EF 将更新所有值类型,但不更新关联。

更常见的方法是这样做:

[HttpPost]
public ActionResult Edit(Product productFromForm)
{
    // Might need this - category might get attached as modified or added
    context.Categories.Attach(productFromForm.Category);

    // This returns a change-tracking proxy if you have that turned on.
    // If not, then changing product.Category will not get tracked...
    var product = context.Products.Find(productFromForm.ProductId);

    // This will attempt to do the model binding and map all the submitted 
    // properties to the tracked entitiy, including the category id.
    if (TryUpdateModel(product))  // Note! Vulnerable to overposting attack.
    {
        context.SaveChanges();
        return RedirectToAction("Index");
    }

    return View();
}

我发现的最不容易出错的解决方案,特别是随着模型变得越来越复杂,有两个方面:

  • 将 DTO 用于任何输入(ProductInput 类)。然后使用 AutoMapper 之类的东西将数据映射到您的域对象。当您开始提交越来越复杂的数据时尤其有用。
  • 在域对象中显式声明外键。即,为您的产品添加一个 CategoryId。将您的输入映射到此属性,而不是关联对象。 拉迪斯拉夫的回答随后的帖子对此进行了更多解释。独立关联和外键都有自己的问题,但到目前为止,我发现外键方法不太麻烦(即,关联实体被标记为已添加、附加顺序、映射前交叉数据库问题等...... )

    public class Product
    {
        // EF will automatically assume FooId is the foreign key for Foo.
        // When mapping input, change this one, not the associated object.
        [Required]
        public int CategoryId { get; set; }
    
        public virtual Category Category { get; set; }
    }
    
于 2012-03-26T17:12:03.257 回答