c# - 无法更新外键引用(导航属性)

标签 c# entity-framework asp.net-mvc-3 repository-pattern entity-framework-ctp5

我知道这似乎是一个重复的条目,但我将所有与我的问题相关的帖子都标记为红色,但我找不到解决方案。我被这个问题困扰了大约一个星期。也许我犯了一些设计问题或者我不知道。问题是我无法更新导航属性,我尝试了几个选项,每次我遇到不同的错误或重复。好的,这是场景:

I have an object "List"
int ID
string Name
int SendType
Category category //these are navigation property
Product product //these are navigation property

Category 和 Product 不知道与 List 的关系。我使用由 CTP5 DbContext 生成器模板生成的 POCO 类,并为每个实体使用一个存储库。在每个存储库中都有对 dbcontext 的引用。

这是 Controller :

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, List list)
    {
        if (!ModelState.IsValid)
            return View(list);

        List originalList = this._listRepository.Get(id);
        TryUpdateModel(originalList, "List", new string[] { "Name", "SendType"});
        //Here's I tried to update manually like this
        if (list.category.id != originalList.category.id)
        {
           Category category = this._categoryRepository.GetNoTracking(list.category.id);
           originalList.Category = category; 
        }
        if (list.product.id != originalList.product.id)
        {
           Product product = this._productRepository.GetNoTracking(list.product.id);
           originalList.Product = product; 
        }
        this._listRepository.Save(); //Here's I call only the SubmitChanges()
        return RedirectToAction("Index");                               
    }

如果我直接尝试,我会收到错误

The entity type 'DbEntityEntry' is not part of the model for the current context

因为我修改了实体类别或产品的状态(它们来自另一个上下文)

如果我提交更改,而不修改关联,我会收到错误

'IDCategory' is a reference key and cannot be updated

等等......任何建议将不胜感激,如果我错了,我也可以修改 View 模型。我没有想法!谢谢

更新

    //This is in List Repository
    public override void Save()
    {           
        checkReferencies();
        db.SaveChanges();            
    }

    private void checkReferencies()
    {          
         foreach (var entry in db.ChangeTracker.Entries()
                               .Where(e => e.State == EntityState.Modified || e.State == EntityState.Added))
         {
             if (ObjectContext.GetObjectType(entry.Entity.GetType()).Name.Equals("List"))
             {
                 /*In this case I tried to update my object directly with the object that comes form the form
                 if (entry.CurrentValues.GetValue<int>("IDList").ToString() != "0" )
                 {
                     db.Entry(entry.Entity).State = EntityState.Modified;
                 }    */                 
                 if (((List)entry.Entity).Product != null)
                 {
                     db.Entry(((List)entry.Entity).Product).State = EntityState.Unchanged;
                 }
                 if (((List)entry.Entity).Category != null)
                 {
                     db.Entry(((List)entry.Entity).Category).State = EntityState.Unchanged;
                 }
             }
             else if (ObjectContext.GetObjectType(entry.Entity.GetType()).Name.Equals("Product") 
                   || ObjectContext.GetObjectType(entry.Entity.GetType()).Name.Equals("Category"))
             {
                 //here I attach the entry that I added
                 db.Category.Attach((Category)db.Entry(entry).Entity.Entity);

             }
         }
    }

    //This is in categoryRepository
    public Category GetNoTracking(int id)
    {            
        return db.Category.AsNoTracking().Single(l => l.IDCategory == id);
    }

    //This is in productRepository
    public Product GetNoTracking(int id)
    {            
        return db.Product.AsNoTracking().Single(l => l.IDProduct == id);
    }

    //This is my DbContext
    namespace BusinessManagerEmail.Models
    {
         using System;
         using System.Data.Entity;

         public partial class DBMailMarketingEntities : DbContext
         {
             public DBMailMarketingEntities()
             : base("name=DBMailMarketingEntities")
             {
             }

             public DbSet<Category> Category { get; set; }
             public DbSet<Customer> Customer { get; set; }
             public DbSet<Subscription> Subscription { get; set; }
             public DbSet<List> List { get; set; }
             public DbSet<Product> Product { get; set; }
        }
   }

这是所有涉及的代码。谢谢!

最佳答案

删除 checkReferencies 因为:

  • List 在你的例子中没有被修改,所以方法的第一部分将不会被执行。
  • Product 无法转换为 Category 并附加到 DbSet。它会抛出异常。
  • Category 已经附加到 Addes 状态(这是错误的,因为它会再次插入类别)。再次附加实体将抛出异常。

只需将 CategoryProduct 附加到 listRepository 的上下文中,然后再将它们设置到 List 中。

或者更好地完全重写您的代码并在存储库之间共享上下文。

关于c# - 无法更新外键引用(导航属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298546/

相关文章:

c# - 返回与该项目相关的所有记录都具有特定条件的项目

asp.net-mvc-3 - 如何在扩展方法中获取 Url.Action

c# - Asp.net MVC3 : is really based on roles @Html. Action ()

c# - 用于登录 ASP.NET 网站的控制台应用程序

c# - 使用 LINQ 创建 JSON

c# - 从多个线程修改 Entity Framework 实体

javascript - ajax中的日期格式

c# - "fire and forget"异步任务或在后台运行它的正确方法

C# 扩展方法 - 返回调用对象

c# - Entity Framework - 代码优先方法