Monday, May 19, 2014

Entity Framework : How to generate model using Entity Framework Design

This is a step-by-step walkthrough which will generate the Model using Entity Framework Designer and then generate a database schema from the model. The model is stored in a an EDMX file

Pre-Requisities

We need to have Visual Studio 2013 installed to complete this walkthrough.(You can use older version too like 2010 or 2012 to complete this tutorial.)
Apart from that, You will also need to have NuGet installed on your Visual Studio.

1. Create new Application

Steps :
  • Create new Application
  • Open Visual Studio, Click File New Project
  • Click Console Application under templates Visual C# Windows
  • Enter Name as ModelFirstApplication and Select OK

2. Create Model
To create model, I will use Entity Framework Designer
  • In Solution Explorer, Right Click on your Project and then Add New Item...
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter Name as Managing Context and click OK
  • Now Select Empty Model from Entity Data Model Wizard and then click Finish.

You will see Entity Framework Designer is opened with a blank model, Now you can start adding entities, properties to the model.
  • Right-click on the design surface and select Add New -> Entity…
  • Enter Manager as the entity name and ManagerId as the key name and click OK
  • Right-click on the new entity on the design surface and select Add New -> Scalar Property, enter ManagerName as the name of the property.
  • Repeat the above Step and a new entity Employee with a EmployeeId as a key property.
  • Add EmployeeName ,Designation as a scalar properties to the Employee Entity.
Now we need to add one to many relationship (Association) between them.
  • Right-click on the design surface and select Add New -> Association…
  • Make one end of the relationship point to Manager with a multiplicity of One and the other end point to Employee with a multiplicity of Many.
  • Check Add foreign key properties to 'Employee' Entity and click OK
Now we can generate a database from the model and can use to read and write data.
Now we can generate a database from the model and can use to read and write data.

3. Generating the database
To generate the database
  • Right-click on the design surface and select Generate Database from Model…
  • Click New Connection… and SQL Express (.\SQLEXPRESS)and enter ModelManager as the database name.
  • Select OK and It will be asked if you want to create a new database, select Yes
  • From choose your version, Select Entity Framework 6.0 and click Next
  • Select Next and the Entity Framework Designer will calculate a script to create the database schema
  • Once the script is displayed, click Finish and the script will be added to your project and opened(In my case it is created as ManagingConext.edmx.sql)
  • Right-click on the script and select Execute, you will be prompted to specify the database to connect to, specify .\SQLEXPRESS.

4. Reading and Writing Data
If you checked the Solution Explorer, The Model are already automatically generated based on EDMX file.
Now its the time to implement the Main method in Program.cs. It will create a new instance of our context and then uses it to insert a new Manager. I have used LINQ query to retrieve all Manager entry from the database.

class Program
{
static void Main(string[] args)
{
using (var db = new ManagingContextContainer())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Manager: ");
var name = Console.ReadLine();

var manager = new Manager { ManagerName = name };
db.Managers.Add(manager);
db.SaveChanges();

// Display all Blogs from the database
var query = from b in db.Managers
orderby b.ManagerName
select b;

Console.WriteLine("All Managers in the database:");
foreach (var item in query)
{
Console.WriteLine(item.ManagerName);
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}

Now run the Application  :


No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...