Introduction
In this article we will create a simple "GuestBook" application using MVC4 and Entity Framework. Steps
First install the "EntityFramework.SqlServerCompact" NuGet package in your application.
Create a simple Model class for our application:
After this package installation, we need to create a Database context class that helps to interact with a database using our GuestBook Model.
The class inherits from the DbContext base class (that resides in the System.Data.Entity namespace), and it begins by declaring a parameterless constructor that uses constructor chaining to pass the name of the database to the base class.
We pass the string "Guestbook" to the base constructor. If we don't do this then the Entity Framework will default to using the full type-name of the context class as the name of the database, and will instead look for a file called "Guestbook.Models.GuestbookContext.sdf".
In the "App_Data" folder we can see the "GuestBook.sdf" file.
We will create a controller that we use in GuestbookContext.
We created two action methods, "index" retrieves the data from the Database and "create" is used to post data to the database.
We also created two simple views to render the Action Results.
Index view to show the "GuestBook" result from the database.
Let's see the results in a browser. The Guest Book create page looks like the following image, we simply enter the data and save that to the local database.
The result page look likes the following image:
That's it. But how is the data is stored in the database? Let's do some analysis. Install the "Glimpse" tool to begin analysis of the MVC application. Check the following link for more information about it.
As you can see, there's no connection string in Web.config, instead we will see the following data provider.
We check the Glimpse and we will get the database details as below. Summary
In this article we saw "Guest Book" application creation in MVC4, used the Entity Framework to work with the database and used "Glimpse" to understand the Entity Framework local database.
View All