EF4 Model First feature demo (CSEFModelFirst)

This example illustrates how to use the new feature - Model First - in the ADO.NET Entity Framework 4.0 to generate DDL that will create a database according to the Entity Data Model.

C# (56.1 KB)
 
 
 
 
 
4 Star
(1)
11,515 times
Add to favorites
5/5/2011
E-mail Twitter del.icio.us Digg Facebook
=============================================================================
     CONSOLE APPLICATION : CSEFModelFirst Project Overview
=============================================================================

/////////////////////////////////////////////////////////////////////////////
Use:

The CSEFModelFirst example illustrates how to use the new feature Model
First in the Entity Framework 4.0 to generate DDL that will create a 
database according to the Entity Data Model.  It also demostrates some 
insert and query operations to test the generated database.


/////////////////////////////////////////////////////////////////////////////
Prerequisite:

NOTE: Please create the database via the database script generated by the 
ModelFirst Entity Data Model.


/////////////////////////////////////////////////////////////////////////////
Creation:

1. Add an ADO.NET Entity Data Model.
   1) Name it as ModeFirst.edmx.
   2) Select the "Empty model" in the "Entity Data Model Wizard".

2. Create the whole Entity Data Model.
   1) Right click the Entity Data Model -> Add -> Entity... to add new Entity
      named Person with Entity Set name People and Entity Key name PersonID.
   2) Right click the Entity Data Model -> Add -> Complex Type to add a new
      Complex Type.  
   3) Set the Complex Type name to Name, add two string typed non-nullable 
      Scalar Properties, FirstName and LastName, to the Complex Type.
   4) Create another Complex Type named Address three string typed nullable 
      properties, City, Country and Zipcode like the step 2) and 3). 
   5) Create two Complex Properties Name, Address in type of Name and 
      Address respectively in the Person Entity.
   6) Create a new Entity named Instructor whose base type is Person.
   7) Add a property named HireDate(DateTime, non-nullable) into the 
      Instructor entity.
   8) Create a new Entity named Student whose base type is Person.
   9) Add three properties, EnrollmentDate(DateTime, non-nullable), Degree
      (Int32, nullable), and Credits(Int32, nullable) into the Student entity.
  10) Create a new Entity named Admin whose base type is Person.
  11) Add a property named AdminDate(DateTime, non-nullable) into the Admin
      entity.
  12) Create a new Entity named BusinessStudent whose base type is Student.
  13) Add a property named BusinessCredits(Int32, nullable) into the
      BusinessStudent entity.
  14) Create a new entity named Department with Entity Set name Departments
      and Entity Key name DepartmentID. 
  15) Add three properties, Name(String, non-nullable), Budget(Decimal, 
      nullable) and StartDate(DateTime, non-nullable) into the Department
	  entity.
  16) Create a new entity named Course with Entity Set name Courses and 
      Entity Key name CourseID. 
  17) Add two properties, Title(String, non-nullable), Credits(Int32, 
      non-nullable) into the Course entity.
  18) Right click the entity Department -> Add -> Association... to add an
      Foreign Key Association between the entities Department and Course.
  19) Set the Department side to 1(One) and Course side to *(Many).
      NOTE: Remember to check "Add foreign key properties to the 'Course' 
      Entity" checked box.  
  20) Modify the Foreign Key property name from DepartmentDepartmentID to
      DepartmentID.  
  21) Add a many-to-many association between the entities Course and 
      Instructor.
  22) Create a new entity named CourseStudent with Entity Set name 
      CourseStudents with no Entity Key.
  23) Add a property, Score(Int32, nullable) into the CourseStudent entity.
  24) Create a one-to-many Foreign Key association between the entities 
      Student and CourseStudent.   
  25) Modify the Foreign Key property name from StudentPersonID to PersonID.
  26) Create a one-to-many Foreign Key association between the entities 
      Course and CourseStudent. 
  27) Modify the Foreign Key property name from CourseCourseID to CourseID.
  28) Set both the properties PersonID and CourseID in the CourseStudent
      entity to Entity Key.
  29) Save the project.

3. Create the DDL of the Entity Data Model.
   1) Right click the Entity Data Model -> Generate Database from Model...
   2) Select the SQL Server instance and database name to create the database.
      (Here we use .\SQLEXPRESS and ModelFirstGeneratedDB)
   3) When the DDL codes are displayed, click Finish.
   4) When a dialog shows to ask you whether to overwrite the existing SSDL and
      MSL, click Yes.

4. Create the database withe DDL.
   1) Open the ModelFirst.edmx.sql.
   2) Execute the database script.


/////////////////////////////////////////////////////////////////////////////
References:

Model First with the Entity Framework 4
http://blogs.msdn.com/adonet/archive/2009/11/05/model-first-with-the-entity-framework-4.aspx

Sneak Preview: Model First in the Entity Framework 4.0
http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-model-first-in-the-entity-framework-4-0.aspx

Entity Framework Design: Model First
http://blogs.msdn.com/efdesign/archive/2008/09/10/model-first.aspx

10-4 Episode 15: Model-First Development with the Entity Framework 4.0
http://channel9.msdn.com/shows/10-4/10-4-Episode-15-Model-First-Development-with-the-Entity-Framework-40/


/////////////////////////////////////////////////////////////////////////////