Creating new records using a web form with Entity Framework
Creating a page to add data to the model
-
Add a new web form to the project called AddBlogEntry.aspx
-
Add text boxes and associated lables to permit the entry of a
title and body of a blog entry
-
-
The title field should be named txtTitle and restricted
to 100 characters using the maxLength property
-
The body field's TextMode should be set to Multiline -
this will ensure it is rendered as a textarea in html
-
Add a button named btnAddEntry and set the text to Add Blog
Entry
-
The code inside your form should look something like this:
<div>
<asp:Label ID="lblTitle" runat="server" Text="Title" AssociatedControlID="txtTitle"></asp:Label>
<asp:TextBox ID="txtTitle" runat="server" MaxLength="100"></asp:TextBox>
</div>
<div>
<asp:Label ID="lblBody" runat="server" Text="Body" AssociatedControlID="txtBody"></asp:Label>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine"></asp:TextBox>
</div>
<asp:Button ID="btnAddEntry" runat="server" Text="Add Blog Entry" />
-
In the designer (in design or split view) double click on the button to add some code. You will be taken to the aspx.cs page, where we will create a new blog entry using the C# Object representation of our database entity
-
Add the following lines of code between the braces for the method for the
btnAddEntry_Click
event
BlogEntry entry = new BlogEntry();
-
You may notice that the BlogEntry class names are underlined.
If this is the case, we need to tell the page where to look to
know what a BlogEntry is. Fortunately Visual Studio can make the
suggestion for us. (If BlogEntry is not underlined, you can skip
the steps in this bit)
-
Click inside BlogEntry, press Ctrl and '.' on the keyboard and select Resolve => using DatabaseModel
-
This will add a line at the top of the file which reads
using DatabaseModel;
-
Note that DatabaseModel is the namespace that our Entity Framework model uses.
-
Add another line of code to save the text from our txtTitle
TextBox as the entry's title as follows:
entry.Title = txtTitle.Text;
-
Add a second line of code which will set the body property of
our blog entry to use the information in the txtBody TextBox.
-
Add a third line of code which will set the date property of
our entry as follows:
entry.Date = DateTime.Now;
-
We now need to access an instance of our Entity Framework
Model so we can save our new BlogEntry to the database. The name
will correspond to the connection string you saved, in this case
DatabaseEntities. Add the following line of code which does just
that:
DatabaseEntities db = new DatabaseEntities();
-
Finally add two lines of code to add our new blog entry to
the BlogEntries table in our database and save it:
db.BlogEntries.Add(entry);
db.SaveChanges();
-
If you see an error on
db.BlogEntries.Add(entry);
change
Add
to AddObject
-
It would be sensible to inform our user that the operation has been successful - this may mean redirecting them to another page, or clearing the input fields.
-
Add some entries to the database using the new form you have created - verify they have been added by viewing the database table data
- Finally you should add some validation to the page so that fields cannot be left blank or have excessive numbers of characters