gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:47 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com