Entity Framework Model First approach, enable to you create your model in Entity Framework designer and then generate SQL file, which will create database schema to match your model .After generating  you execute the SQL file to create the schema in your database. The classes that you communicate with in your application are automatically generated from the EDMX file.

So, Entity Framework Model First approach, add new ADO.NET Entity Data Model and Select Empty EF Designer model in Entity Data Model Wizard. Following is a simple example of creating a new MVC 4 project using Model First approach.

 Select ADO.NET Entity Data Model from wizard and enter name Article in the Name field.

What is Model First Entity Framework

Now Click on Empty Entity Data Model Wizard dialog.

What is Model First Entity Framework

Right-click on the designer and select Add New  EntityWhat is Model First Entity Framework

Add Entity Name as shown in the following image.


What is Model First Entity Framework

Show below your entity created:

What is Model First Entity Framework

Now Right-click on the add new entity on the design table and select Add New  Scalar Property, enter Name as the name of the property.

What is Model First Entity Framework

Enter your property name show below:

What is Model First Entity Framework

After creating property name your entity looks like below:

What is Model First Entity Framework

After creating the required entities, right click on designer > click to Generate database from model.

What is Model First Entity Framework

Following open Generate Database Wizard. You can select existing database or create a new connection by clicking on New Connection. 

What is Model First Entity Framework

Select database server and enter the name of the database with credential to create and then click OK.

What is Model First Entity Framework

After clicking ok it will ask you for the confirmation for creating a new database. Click yes to create a database.

What is Model First Entity Framework

If you want to save connection string check save entity checkbox and click on next button.

What is Model First Entity Framework

Now Select your targeted entity framework and click on next button.

What is Model First Entity Framework

Click finish to generate DDL for the DB model as shown below.

What is Model First Entity Framework

Now go to solution explorer and Right click on article.edmx.sql to execute script

What is Model First Entity Framework

Now Server connection dialog will open where enter your database login details.

What is Model First Entity Framework

After executed see your table has been created as show below.

What is Model First Entity Framework

Now Use blogs table to insert a record with entity framework.

         ArticleContainer db = new ArticleContainer();
            Blog blog = new Blog();
            blog.Title = "what is mvc";
            blog.Description = "test";
            db.Blogs.Add(blog);
            db.SaveChanges();

Now select your table see record inserted.

 select * from Blogs

result:

What is Model First Entity Framework

Thanks.


0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like