0

I am using Entity Framework in my MVC WebApp I can update modified entries but I can't add an relationship.

I know similar questions have been solved here. But I could't find exact answer. I cant update entity

I have an entity cart load.

public class CartLoad
{
public int CartLoadID{get; set;}
public string Description{get; set;}

..... some more properties

public virtual User Driver { get; set; }
}

And a ViewModel which passes data to controller just fine.

public class CartLoadEditView
{
    public CartLoad CartLoad { get; set; }
    public string DriverID { get; set; }
    public SelectList Drivers { get; set; }
}

In but then in controller when I save model all properties of CartLoad are updated except the Driver. Which is and User in table of Users created by Microsoft.Indentity framework.

Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit(CartLoadEditView CartLoadEdit)
    {
        CartLoad cartLoad = new CartLoad();

        if (ModelState.IsValid)
        {
            User CLDriver = await usrManager.FindByIdAsync(CartLoadEdit.DriverID);
            CartLoadEdit.CartLoad.Driver = CLDriver;

            db.CartLoads.Attach(cartLoad);
            cartLoad = CartLoadEdit.CartLoad;

            db.Entry(cartLoad).State = EntityState.Modified;

            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
         ....

When I add breakpoint to

db.Entry(cartLoad).State = EntityState.Modified;

I can see that cartLoad.Driver property is filled with my selected driver but it wont be updated in DB

Solution which I found is to add DriverID property to CartLoad entity and update that. Is that really the right Solution? When EF already added DriverId property to CartLoad entity when using CodeFirst method.

Thanks.

5
  • just remove this line and it will works: db.Entry(cartLoad).State = EntityState.Modified; if this solve your problem then i will post it as answer Commented May 19, 2016 at 16:06
  • I sorry to say this but the result is the same. Commented May 19, 2016 at 16:11
  • I'm try to understand what you want to do: why you are creating a new CartLoad ! just add the driver to CartLoadEdit.Cartload.Driver and change it state to Modified! remove all cartload and just change state of the db.Entry to CartLoadEdit.CartLoad to modified Commented May 19, 2016 at 16:24
  • I created new CartLoad just for code readability. Even if I dont use new CartLoad and use CartLoadEdit then the result is still the same. I will update CartLoad properties but not the foreign key. Commented May 19, 2016 at 16:34
  • I guess I should do something like this. stackoverflow.com/a/27761538/3431641 Commented May 19, 2016 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.