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



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();
}
}
}