Entity Framework – Model First Approach Example


In this article, Let’s see an example of Entity Framework – Model First approach

If you don’t know about Entity Framework, please go through this article.

Model First allows you to create a new model using the Entity Framework Designer and then generate a database schema from the model.

Entity Framework – Model First Approach Example

1. Let’s create a console application. Name it as “EFModelFirstExample”.

Add a reference to the “Entity Framework” using nuget package manager. If you don’t know how to add a reference using Nuget, please refer this post.

2. We will create a model by using Entity Framework Designer. Let’s add a new item to our project “ADO.NET Entity Data Model”.

3. Once you click on “Add”, there are 2 options, either you can create a model using existing database or you can start it by creating empty model. In this project, we will create an empty model.

4. Once you “Finish” it, you will see that an empty file called “Model1.edmx” is added into your project. Open it and right click on it and select Add New and click on Entity to create a new entity.

5. For this example, we will consider a simple Company-employee relationship and create entities for them.

6. Right click on it and Add new scalar property called “Name” and “Address”.

7. Now follow the same procedure for “Employee” entity. Add “Name” and “Age” scalar properties to it.

Once it’s done, we need to set the association between those 2 entities. Right click the “Company” entity and click “Add New” – “Association”.

8. Check the “Multiplicity” option and set it for our entities. In our case, it’s one to many. (a company – many employees)

9. Entities and their relationship will look like as show in the screenshot below. Just right click on the “edmx” file anywhere on the screen and click on “Generate Database from Modal” to generate database for our defined entities.

10. Once you click on “Generate Database from Modal”, you will be asked to set connection string for your database.

11. Then from the following screen, select “Microsoft SQL Server” and click “Continue”.

12. Enter the Server Name, credentials and Database name.

13. If you are creating a new database, you will see following screen. Just click “Ok” as it will create a new database only.

14. Once done, select “Yes” or “No” (include or exclude sensitive data in connection string) and Click “Next” button.

15. Finally review “Summary and Settings” and click on “Finish” button.

16. You may see following screen once you finish the wizard. Just ignore it and click “Ok”.

17. A file called “Model1.edmx.sql” will be created and added to the project. Open it and right click, then select “Execute”. It will create tables and constraints in the database.

18. Since entities are automatically created, we can directly use it in the code.

If you just expand the “Model1.tt”, you can see the classes “Company.cs” and “Employee.cs” are auto created during the process.

19. Now let’s write a sample code which will add a company record and display it on the screen.


Console.WriteLine("Please enter a company name.");
string companyName = Console.ReadLine();

Console.WriteLine("Please enter a company address");
string companyAddress = Console.ReadLine();

EFModelFirstExample.Company company = new Company();
company.Name = companyName;
company.Address = companyAddress;
using (var db = new DBContext())
{
db.Companies.Add(company);
db.SaveChanges();
}

Console.WriteLine("List of companies in the DB");
using (var db = new DBContext())
{
var companies = (from c in db.Companies
select c.Name).ToList();
foreach (var name in companies)
{
Console.WriteLine(name);
}
Console.ReadKey();
}

Hope this helps. Please comment below the post if you have any doubts or questions.

Source Code Download:

Github [Repository Link]

Box.com [Direct Download Link]

 

Advertisements

2 thoughts on “Entity Framework – Model First Approach Example

  1. Pingback: Entity Framework – Database First Approach Example | A Programming Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s