在 Entity Framework 中更新导航 属性
Update Navigation Property in Entity Framework
我是深度新人Entity Framework
我只是想知道为什么 Entity Framework 不保存更改,尤其是导航 属性 尽管所有其他属性都已更新
请我想要简单的解释
这是我的服务Class
public class ProductsService
{
AppDbContext _Context;
public ProductsService()
{
_Context = new AppDbContext();
}
public Product GetProduct(int id)
{
return _Context.Products.Include(p=>p.Category).Where(pro =>pro.Id == id).SingleOrDefault();
}
public void UpdateProduct(Product product)
{
_Context.Entry(product).State = System.Data.Entity.EntityState.Modified;
_Context.SaveChanges();
}
}
在控制器中:
[HttpPost]
public ActionResult Edit(NewCategoryViewModel pro,int Id)
{
CategoriesService ser = new CategoriesService();
var NewProduct = ProService.GetProduct(Id);
var NewCat = ser.GetCategory(pro.CategoryId);
NewProduct.Description = pro.Description;
NewProduct.Name = pro.Name;
NewProduct.Price = pro.Price;
NewProduct.Category = NewCat;
ProService.UpdateCategory(NewProduct);
return RedirectToAction("ProductTable");
}
我已经试过了,效果很好
[HttpPost]
public ActionResult Edit(NewCategoryViewModel pro,int Id)
{
using (var Context = new AppDbContext())
{
var NewProd = Context.Products.FirstOrDefault(pr => pr.Id == Id);
var Cat = Context.Categories.FirstOrDefault(cat => cat.Id == pro.CategoryId);
Context.Entry(NewProd).State = EntityState.Modified;
NewProd.Name = pro.Name;
NewProd.Description = pro.Description;
NewProd.Price = pro.Price;
NewProd.Category = Cat;
Context.SaveChanges();
}
}
和 UpdateCategory
public void UpdateCategory(Category category)
{
using (var Context = new AppDbContext())
{
Context.Entry(category).State = System.Data.Entity.EntityState.Modified;
Context.SaveChanges();
}
}
为什么第一个不行
我知道可能是导航状态的问题属性
您可以考虑使用 .add() 而不是 .entry()。
.add() 还将跟踪其他可达的实体。
可在此处找到文档:
entity framework
由于您在 ProductService
中创建了 DbContext
并在其中创建了一个新上下文:
public void UpdateCategory(Category category)
{
using (var Context = new AppDbContext())
{
Context.Entry(category).State = System.Data.Entity.EntityState.Modified;
Context.SaveChanges();
}
}
-> 您同时使用两个不同的 DbContext
(这可能会导致更改跟踪出现问题)!
解法:
尝试对所有 DbContext
使用 DependencyInjection 而不是在本地创建它们以防止更改跟踪出现问题。
我是深度新人Entity Framework 我只是想知道为什么 Entity Framework 不保存更改,尤其是导航 属性 尽管所有其他属性都已更新 请我想要简单的解释
这是我的服务Class
public class ProductsService
{
AppDbContext _Context;
public ProductsService()
{
_Context = new AppDbContext();
}
public Product GetProduct(int id)
{
return _Context.Products.Include(p=>p.Category).Where(pro =>pro.Id == id).SingleOrDefault();
}
public void UpdateProduct(Product product)
{
_Context.Entry(product).State = System.Data.Entity.EntityState.Modified;
_Context.SaveChanges();
}
}
在控制器中:
[HttpPost]
public ActionResult Edit(NewCategoryViewModel pro,int Id)
{
CategoriesService ser = new CategoriesService();
var NewProduct = ProService.GetProduct(Id);
var NewCat = ser.GetCategory(pro.CategoryId);
NewProduct.Description = pro.Description;
NewProduct.Name = pro.Name;
NewProduct.Price = pro.Price;
NewProduct.Category = NewCat;
ProService.UpdateCategory(NewProduct);
return RedirectToAction("ProductTable");
}
我已经试过了,效果很好
[HttpPost]
public ActionResult Edit(NewCategoryViewModel pro,int Id)
{
using (var Context = new AppDbContext())
{
var NewProd = Context.Products.FirstOrDefault(pr => pr.Id == Id);
var Cat = Context.Categories.FirstOrDefault(cat => cat.Id == pro.CategoryId);
Context.Entry(NewProd).State = EntityState.Modified;
NewProd.Name = pro.Name;
NewProd.Description = pro.Description;
NewProd.Price = pro.Price;
NewProd.Category = Cat;
Context.SaveChanges();
}
}
和 UpdateCategory
public void UpdateCategory(Category category)
{
using (var Context = new AppDbContext())
{
Context.Entry(category).State = System.Data.Entity.EntityState.Modified;
Context.SaveChanges();
}
}
为什么第一个不行 我知道可能是导航状态的问题属性
您可以考虑使用 .add() 而不是 .entry()。 .add() 还将跟踪其他可达的实体。
可在此处找到文档: entity framework
由于您在 ProductService
中创建了 DbContext
并在其中创建了一个新上下文:
public void UpdateCategory(Category category)
{
using (var Context = new AppDbContext())
{
Context.Entry(category).State = System.Data.Entity.EntityState.Modified;
Context.SaveChanges();
}
}
-> 您同时使用两个不同的 DbContext
(这可能会导致更改跟踪出现问题)!
解法:
尝试对所有 DbContext
使用 DependencyInjection 而不是在本地创建它们以防止更改跟踪出现问题。