Tech Junkie Blog - Real World Tutorials, Happy Coding!: Entity Framework (Database First) Part 2: Creating Entity Model From an Existing Database Entity Framework 6.1.1

Wednesday, February 11, 2015

Entity Framework (Database First) Part 2: Creating Entity Model From an Existing Database Entity Framework 6.1.1

This is part two of our series on Entity Framework, if you would like to catch up with what we did on on part one, feel free to go over the lesson so that you can follow along.

In the last part we installed Entity Framework 6.1.1 with NuGet package management tool in Visual Studio.  In this lesson we will learn to create an Entity Model using the Northwind database.  Follow the steps below.

  1. Add a new folder call "Models" in the "Northwind" database
  2.  
     
     
     
     
     

    2. Right-click on the "Models" folder and select "Add", the select "New Item"

    New Item Visual Studio 
    3.   Select "Data" on the left tree menu under C#, then in the main menu select "ADO.NET Entity Model".  In the "Name" field type "NorthwindModel".  Click "Add" when you are done.
NorthwindModel

    4.  On the "Entity Data Model Wizard" screen select "EF Designer from database" and then select "Next"

EF Designer from database
    5. Click on the "New Connection" button

Choose Your Data Connection

    6.  In the "Server name: " field type in your hostname and database instance.  Then select "Northwind" as the database.  Then click "OK"
Connection Properties

    7.  Check the "Save connection settings in Web.Config as: "  Type in "NorthwindEntities" in the textbox.  Then click "Next"

NorthwindEntities

    8.  If there's a choice on which Entity Framework to use, select "Entity Framework 6.x" then click "Next"
Choose Your Version
    9. Select all of the Northwind database tables and select "Pluralize or singularize generated object names" and "Include foreign key columns in the model".  Type "NorthwindModel" in the "Model namespace" field.  Then click "Finish"
     



The Northwind Entity Model is created
Northwind Entity Model is created

The Entity model context code files are also generated as well. To open the Entity Model designer screen just double click on the NorthwindModel.edmx file

NorthwindModel.edmx

If you look the Web.config file you will see it has been modified significantly with Entity Framework settings without you even opening it. It's all done for you automatically for you.

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, 
visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.
ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral,
 PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <connectionStrings>
    <add name="NorthwindEntities" connectionString="
metadata=res://*/Models.NorthwindModel.csdl|res://*/Models.NorthwindModel.ssdl
|res://*/Models.NorthwindModel.msl;
provider=System.Data.SqlClient;provider connection string=&quot;
data source=HP-LAPTOP\SQLSERVERDEV;initial 
catalog=Northwind;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
 providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,
 EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient"
 type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Blogs in the Entity Framework Series:

  1. Installing Entity Framework 6.1.1 With NuGet
  2. Creating Entity Model From an Existing Database Entity Framework 6.1.1
  3. Using the Entity Framework Objects In ASP.NET Project
  4. Entity Framework (Database First) Part 4: Using the LINQ and Projection To SELECT Columns From Entities


2 comments:

Search This Blog