Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Saving changes with EfCodeFirst business wrapper?

$
0
0
Saving changes with EfCodeFirst business wrapper?
West Wind Web Toolkit for ASP.NET
Saving changes with EfCodeFirst business wrapper?
Dec. 14, 2012
07:48 am
3O10GR4YEShow this entire thread in new window
Gratar Image based on email address
From:Matt Slay
To:Rick Strahl
Rick - I have a basic MVC4 CRUD app up and running, but I cannot figure out how to call the Save() method from the post-back of the Edit action on my controller.

If I understand this EF stuff properly, somehow, I've got to get the Quote entity back into the EfCodeFirst Context, and mark it as "modified" so it will be saved back to the database. I just cannot figure out the right sequence. I've tried to "attach" the Quote entity but that fails for some reason, and when I try calling .Save(quote), it does not give an error, but yet the changes do not get saved.

Here's what I've tried:

[HttpPost]public ActionResult Edit(Quote quote) {try {if (ModelState.IsValid) { var quoteBO = new busQuote();//quoteBO.Attach(quote);//quoteBO.Context.Entry(quote).State = System.Data.EntityState.Modified; quoteBO.Entity = quote; quoteBO.Save(quote);return RedirectToAction("Index"); } return RedirectToAction("Index"); }catch {return View(); } }

The Quote entity is a complex entity, as it has a related Customer object, and a child collection of line items:

Quote entity has:
- various properties from its own table field
- has a Customer object (per PK/FK relationship defined in model attributes)
- has a collection of QuoteItems which are line items on the Quote.

Eventually, I will need to save changes to the QuoteItems collection as well, but for now, I'm just wanting to accomplish saving basic properties on the top Entity in the busQuote object (i.e. the to the Quote table).

Here's the busQuote class:

publicclass busQuote : EfCodeFirstBusinessBase<Quote, QuoteContext> { .... }

Here's the Context:

publicpartialclass QuoteContext: EfCodeFirstContext {public DbSet<Quote> Quotes { get; set; } public DbSet<QquoteItem> QuoteItems { get; set; }public DbSet<Customer> Customers { get; set; }protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new quoteMap()); modelBuilder.Configurations.Add(new quoteitemMap()); modelBuilder.Configurations.Add(new customerMap()); } }



Viewing all articles
Browse latest Browse all 10393

Trending Articles