0

I have a viewModel that contains two classes...

public class vwbooking
{
    public booking bookings { get; set; }
    public IEnumerable<trace> traces { get; set; }
}

Booking and trace are entities in an edmx.

I want to update the data in these two class with one call to save.

This is what I've tried, along with several other unsuccessful "shot-in-the-dark" variants...

public ActionResult Edit(vwbooking vwbooking)
{
if (ModelState.IsValid)
{
    DbEntityEntry<vwbooking> entry = db.Entry(vwbooking);
    entry.Property(e => e.bookings.firstname).IsModified = true;

    db.SaveChanges();
}
}

I get the following error when calling the save method...

The entity type vwbooking is not part of the model for the current context.

The GET method loads successfully. The post is where I'm having trouble. This is the GET method...

public ActionResult Edit(int id = 0)
    {
        booking booking = db.bookings.Find(id);
        var viewModel = new vwbooking();
        viewModel.bookings = booking;
        viewModel.traces = (from l in db.traces where l.bookingid == booking.bookingid select l);
        return View(viewModel);
    }

This is my db context class

public class salesContext : DbContext
{
    public salesContext() : base()
    {
        Configuration.LazyLoadingEnabled = true;
    }

    public salesContext(string Connection) : base(Connection)
    {
        Configuration.LazyLoadingEnabled = true;
    }

    public DbSet<booking> bookings { get; set; }
    public DbSet<trace> traces { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<booking>().HasKey(e => e.bookingid);
        modelBuilder.Entity<trace>().HasKey(e => e.traceid);
    }
}
3
  • 1
    This is because vwbooking is not a model in the DbContext. It just hold the data for your view to display. You have to update the vwbooking properties because those make your model.
    – dvjanm
    Oct 17, 2013 at 13:56
  • ok - I think I understand, but I thought that's what I was doing? What should I do differently?
    – tintyethan
    Oct 17, 2013 at 14:01
  • I have added more details below in my answer
    – dvjanm
    Oct 17, 2013 at 14:08

1 Answer 1

1

The code for update your model is:

db.Attach(vwbooking.bookings)
db.ObjectStateManager.ChangeObjectState(vwbooking.bookings, System.Data.EntityState.Modified)

vwbooking.traces.ToList().ForEach(
  t =>
  {
  db.Attach(t);
  db.ObjectStateManager.ChangeObjectState(t, System.Data.EntityState.Modified);
  }
);

db.SaveChanges();

try this code in the Edit Controller

3
  • this is very close. The booking part will update. The traces part is throwing a "Value cannot be null.Parameter name: source" error. I'm still trying to work through it.
    – tintyethan
    Oct 17, 2013 at 14:41
  • I haven't been able to figure it out. Even this throws the same error... vwbooking.traces.ToList().ForEach(t => { Response.Write("qwer"); });
    – tintyethan
    Oct 17, 2013 at 16:42
  • I'm going to start a new thread based on your feedback. Thanks for the help. The new thread is stackoverflow.com/questions/19432776/…
    – tintyethan
    Oct 17, 2013 at 16:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.