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

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:

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:

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

<asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" DefaultMode="Insert" runat="server" InsertMethod="DetailsView1_InsertItem">
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
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 Spaanjaars
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)