View Single Post
  #6 (permalink)  
Old October 28th, 2013, 04:28 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
However, the Code First approach for generating the DB is not something I can relate to very well.
For green field applications, I like this model a lot. I talk about it extensively in this eBook / article series: http://imar.spaanjaars.com/573/aspne...duction-part-1

Quote:
Although that gets us really off-topic - my point is that I'd love to see some of your great detailed examples for using the Model Binding approach by generating and modifying code from an existing DB.
For existing databases, database first is a suitable model as well. In my updated book for 4.5.1, I use database first as well, just as in earlier editions. I basically generate a new edmx file from a database (using EF 6) and then use model binding to the entities. For example, here's how a new PhotoAlbum gets inserted:

Code:
public void DetailsView1_InsertItem()
{
  PhotoAlbum photoAlbum = new PhotoAlbum();
  TryUpdateModel(photoAlbum);
  if (ModelState.IsValid)
  {
    using (var myEntities = new PlanetWroxEntities())
    {
      myEntities.PhotoAlbums.Add(photoAlbum);
      photoAlbum.UserName = User.Identity.Name;
      myEntities.SaveChanges();
    }
    Response.Redirect(string.Format("ManagePhotoAlbum?PhotoAlbumId={0}", photoAlbum.Id.ToString()));
  }
}
with this markup:

Code:
<asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" DefaultMode="Insert" runat="server" InsertMethod="DetailsView1_InsertItem">
  <Fields>
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
  </Fields>
</asp:DetailsView>
Does this help? I can supply you with other examples as well if you have specific requests, but I don't want to replicate my entire book here just yet ;-) Also, some examples may not make sense without the explanation from the book.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
daveharney (October 29th, 2013)