Entity Framework Database First

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

In this we made the database first and than it auto generate the model and its entities from database as follows:

Entity Frame Database First Approach Model
1) Create Entity Frame Database First Approach Model

– Right click on your project -> Add new item -> Select ADO.NET Entity Data Model -> Name it Model1.edmx

– Follow instruction on the wizard, select the server -> select database -> select tables you want to add to the model

1) By default this will create a model with Entity Container Name as DatabaseName+Entity

– For my case, my database is DiTran, so it’s DiTranEntities

2) Namespace for the model will be DatabaseName + Model

– For my case, it’s DiTranModel

If you open ‘Model1.Designer.cs’ file, you will see all the properties, DbContext, DbSet that we need to establish link between model and database, but you know this is bit complicated lines or call it ‘not clean’. Don’t worry, we can create a cleaner version of this file. How?
You can do this by right clicking on ‘Model1.edmx’ file and selecting ‘Add Code Generation Item’ and from the window install ‘EF DbContext Generator for C#’. Follow the image.
Now, you will see much cleaner code, like we seen in ‘Code First’ post. Look at the screen.

Now we are ready to add Controllers and appropriate Views in the project which will be exactly same as we did in  ‘Code First Approach in Entity Framework

2) Try to retrieve test data:

Using the code below.

NOTE NOTE: Connection String for entity framework data context is different than regular sqlClient connection string. I’ve created a method to generate Entity Framework connection string as CreateEntityConnectionString().

/*

* Author: Di Tran

* Date: 07-25-2011

* Description: Get test data

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DiTran.DataManager.Data;

using System.Data.SqlClient;

using System.Data.EntityClient;

using System.Configuration;

namespace DiTran.DataManager

{

public class DiTranDataManager

{

private tblEmployee _employee;

public DiTranDataManager()

{

}

public tblEmployee GetEmployee()

{

string conString = this.CreateEntityConnectionString();

DiTranEntities1 _theEntities = new DiTranEntities1(conString);

this._employee = _theEntities.tblEmployees.FirstOrDefault();

return this._employee;

}

public string CreateEntityConnectionString()

{

// Specify the provider name, server and database.

string providerName = “System.Data.SqlClient”;

string serverName = @”ServerName”;

string databaseName = “DiTranDatabase”;

// Initialize the EntityConnectionStringBuilder.

EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder()

{

Provider = providerName,

ProviderConnectionString = new SqlConnectionStringBuilder()

{

DataSource = serverName,

InitialCatalog = databaseName,

IntegratedSecurity = true

}.ToString()

};

// Set the Metadata location.

entityBuilder.Metadata = @”res://*/Data.Model1.csdl|

res://*/Data.Model1.ssdl|

res://*/Data.Model1.msl”;

return entityBuilder.ToString();

}

}

}


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.