How do I map tables to entity entities from an existing database?
The entity Framework provides an easy way to create entities class for all tables and views in an existing database, and can be configured with the Dataannotation feature and the Fluent API.
First, right-click your Visual Studio project, add-New project:
Select ADO. NET Entity Data Model (ADO) and specify the model name (this name will be the class name of the Contenxt Class), click Add.
You will then open the wizard page of the Entity Data model, select Code first from database, and then click Next.
If the drop-down list does not contain the database you want to include, click New Connection to create a database link for the existing database. Select OK, and click Next.
Select the table or view you want to map into a class, and click Done.
EF then creates an entity class for all of the tables or views you choose.
As shown below, each time you map a database in, EF creates a corresponding context class (using the Fluent API configuration).
namespaceefdemo{usingSystem; usingSystem.Data.Entity; usingSystem.ComponentModel.DataAnnotations.Schema; usingSystem.Linq; Public Partial classSchoolcontext:dbcontext { PublicSchoolcontext ():Base("name=schoolcontext2") { } Public VirtualDbset<course> Courses {Get;Set; } Public VirtualDbset<standard> Standards {Get;Set; } Public VirtualDbset<student> Students {Get;Set; } Public VirtualDbset<studentaddress> studentaddresses {Get;Set; } Public VirtualDbset<teacher> Teachers {Get;Set; } Public VirtualDbset<view_studentcourse> View_studentcourse {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelbuilder.entity<Course>() . Property (e=e.coursename). Isunicode (false); Modelbuilder.entity<Course>() . Hasmany (e=e.students). Withmany (e=e.courses). Map (M= M.totable ("Studentcourse"). Mapleftkey ("CourseID"). Maprightkey ("StudentID")); Modelbuilder.entity<Standard>() . Property (e=e.standardname). Isunicode (false); Modelbuilder.entity<Standard>() . Property (e=e.description). Isunicode (false); Modelbuilder.entity<Standard>() . Hasmany (e=e.students). Withoptional (e=E.standard). Willcascadeondelete (); Modelbuilder.entity<Standard>() . Hasmany (e=e.teachers). Withoptional (e=E.standard). Willcascadeondelete (); Modelbuilder.entity<Student>() . Property (e=e.studentname). Isunicode (false); Modelbuilder.entity<Student>() . Property (e=e.rowversion). Isfixedlength (); Modelbuilder.entity<Student>() . Hasoptional (e=e.studentaddress). Withrequired (e=e.student). Willcascadeondelete (); Modelbuilder.entity<StudentAddress>() . Property (e=e.address1). Isunicode (false); Modelbuilder.entity<StudentAddress>() . Property (e=e.address2). Isunicode (false); Modelbuilder.entity<StudentAddress>() . Property (e=e.city). Isunicode (false); Modelbuilder.entity<StudentAddress>() . Property (e=e.state). Isunicode (false); Modelbuilder.entity<Teacher>() . Property (e=e.teachername). Isunicode (false); Modelbuilder.entity<Teacher>() . Hasmany (e=e.courses). Withoptional (e=e.teacher). Willcascadeondelete (); Modelbuilder.entity<View_StudentCourse>() . Property (e=e.studentname). Isunicode (false); Modelbuilder.entity<View_StudentCourse>() . Property (e=e.coursename). Isunicode (false); } }}
EntityFramework Code-first Easy Tutorial (11)-------map out tables from an existing database