EntityFramework Code-first Easy Tutorial (11)-------map out tables from an existing database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.