Update Navigation Property in Entity Framework

c# entity-framework entity-framework-6 lazy-loading

Question

I am a new at depth of the Entity Framework I have just wondered why Entity Framework doesn't save changes especially the navigation property although all other properties are already updated Please I want simple explanation

This is My Service 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();
    }
}

In Controller:

[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");
}

I have tried this and it works fine

[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();
    }
}

and for UpdateCategory

public void UpdateCategory(Category category)
{
    using (var Context = new AppDbContext())
    {
        Context.Entry(category).State = System.Data.Entity.EntityState.Modified;
        Context.SaveChanges();
    }
} 

Why the first one Not work I know may be the problem in the state of the navigation property

1
1
9/4/2019 11:20:15 AM

Accepted Answer

Since you created the DbContext inside ProductService and you created a new Context inside:

public void UpdateCategory(Category category) 
{ 
    using (var Context = new AppDbContext()) 
    { 
        Context.Entry(category).State = System.Data.Entity.EntityState.Modified; 
        Context.SaveChanges(); 
    }
}

-> you use two different DbContext's together (which can cause problems with change tracking)!

Solution: Try to use DependencyInjection for all DbContext's instead of creating them locally to prevent problems with change tracking.

0
9/4/2019 11:42:04 AM

Popular Answer

You might consider using .add() instead of .entry(). .add() will also track other reachable entities.

documentation can be found here: entity framework



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow