Introduction
ADO.NET Entity Framework is an ORM, Object Relational Mapping. It basically generates business objects and entities depending on the database tables.
What is ORM
In simple words, ORM maps our model class properties with the database table.
There are three approaches in accessing the database using Entity Framework.
- Database first
- Model First
- Code First
The purpose of this article introduces yourself to the Code first approach.
Topics to be covered
- Introduction to Entity Framework code first.
- CRUD operations using Entity Framework Code first.
- Data Annotations and Updating the database on modelchanges
- Creating repository with MVC that makes use of Entity Framework.
- Creating relationships (one-many, many – many, one –one) between tables using code first approach.
- Inheritance in Entity Framework.
I will be covering the topic 1 in this article and then the further topics will be covered in the next set of articles.
Pre-requisites
You will need to have Visual Studio 2010 or higher installed to complete this walk-through.
If you are using Visual Studio 2010, you also need to have Nuget installed.
I will be covering the topic 1 in this article and then the further topics will be covered in the next set.
Let's start
Fire up Visual Studio, I will be using Visual Studio 2012 for this article, you can use the one of your choice.
Go to File, New, then Project and select C# and select Web in the templates, then select ASP.NET MVC4 Web Application.
Name the application “CodeFirstDemo”.
From the following model box, select Internet Application and click OK.
Our application environment is ready to go further.
From the Solution Explorer, right-click the Models folder and add a class, name the class Artist.cs.
Add two properties, AritistID and ArtistName as in the following screenshot:
Add another class to the Models folder and name it CodeFirstDataContext.cs.
Inherit from the DbContext class into the codeFirstDataContext.cs and add the following property.
public DbSet<Artist> Artists {get;set;}
Our Data Context class will look like this, ensure you add the reference to System.Data.Entity.
Right-click the solution and build it.
Our Model is ready!
Next we need to add a controller and a view to present the artists on the browser.
Let's create a controller by right-clicking on the controller folder then select Add Controller.
Name the controller ArtistController and from the template, choose Empty MVC controller.
Click Add.
Reference the model folder as in the following ArtistController.
using CodeFirstDemo.Models;
And then instantiate the datacontext we created in the Model folder as in the following:
CodeFirstDataContext context = new CodeFirstDataContext();
And then in the index view add the code to return the view with the list of artists.
Our ArtistController class will look like the following:
Now, let's add our index view to list the artists.
Right-click on the index method in ArtistController and click Add View.
The name of the view will be automatically displayed as Index. Select the strongly-typed view and artist as our Model class and from the scaffold option select list.
Click Add. An Index view will be generated as in the following screenshot:
Click F5 to run the application and open the Artist/Index in the browser.
If you get this page, we are done.
Now let's create some artists. If you click create new link in the index, you will get an error, obviously because there is no code written and no matching view found for creating a new link.
Before that, the question is, where is the database?
Stop the application and go to Solution Explorer, click the icon as in the following image. Show all files on top of the Solution Explorer.
Now you will find a database file in the app-data folder.
Double-click on it, it will open the file in the Server Explorer, drill down into it.
Wow, everything is there! How did that happen?
The Entity Framework created a database automatically from the model class artist using the datacontext we wrote. DbContext is the class that we inherited to our modelContext created a database for us based on the information we provided.
By convention, the name of the class becomes the table name and the properties become the columns.
ArtistID becomes the primary key that is the convention followed by Entity Framework if a field name has the suffix ID with a class name or just an ID, the DBContext class will automatically consider that field to br the primary key.
- If a local SQL Express instance is available (installed by default with Visual Studio 2010) then Code First has created the database on that instance.
- If SQL Express isn't available then Code First will try and use LocalDb (installed by default with Visual Studio 2012).
- The database is named after the fully-qualified name of the derived context, in our case that is CodeFirstDemo.Models.CodeFirstDataContext.
We can also have our own database server and we can pass that connection string to the web.config file and we can reference that in the DataContext class as in the following code snippet:
Now let's add a create method to the Artist Controller.
Right-click on the Create method and add a view and select Create in the scaffold template as in the following screenshot:
Click Add.
Create View is generated as in the following code snippet:
Run the application and open the index page, click on the create new link, the browser will present the page as in the following screenshot:
Now we need to write a controller method for the create method to post the data back to the database. Let's do that!
Open the Artist controller and add a HTTP post method for
Create.Run the application and create a new artist. Once the artist is created it will redirect to the index page and show the created artist as in the following screenshot:
Conclusion
We defined a model using a class. We created a datacontext for that model class that creates a database for us based on the default connection string provided in the web.config. We created a controller for Artist and displayed the index for the list of artists. Also, we made a create view for creating the artists and wrote the create post method to save the artist back to the database using the DataContext Class.
I will be writing the next article that further explains the DataAnnotations and how to make a repository in Entity Framework.
I hope you enjoyed the article. Share your comments or views.