Entity Framework 6.1-model First

Source: Internet
Author: User

Original: Entity Framework 6.1-model First

Model first-, as its name implies, is to create the EF data model and generate the EF creation of the database through the data model.

Steps.

1. Create a new Dal folder, right-click Add New item on the folder, open the new Item form, and select data->ado. NET Entity Data model. Fill in the Data model name and click Add



2. The Create Wizard form is displayed. Select an empty model. Click Finish



3. Open the Data Model editing window and start editing the data model



4. Create a new empty database, on the Data Model View page, right-click Generate database from model, open the Build Database Wizard interface



5. Select New connection, establish a connection to the new database, and click Next. Generate DDL (build database SQL)



6. Click Finish to build the database. The corresponding database connection string associated with the DbContext is also generated in the build Dbcontext,web.config.



7. If there is no auto-build, you can execute the built-in SQL in the open demx.sql file



8. Here the EF model is created, and you can use the following code to test whether the creation was successful.

using (Modelfirsttest1container db = new Modelfirsttest1container ())
{

var stu1=new Student
{
Sex = "Male",
Stuname = "Huang Yong"
};
var stu2 = new Student
{
Sex = "Female",
Stuname = "Lily"
};
var class1 = new Sclass
{
CName = "First Grade One class in high school",
Headteacher = "Miao Jian Guang",
Student = new List<student> () {stu1, STU2}
};
Db. Sclassset.add (Class1);
Db. SaveChanges ();
}



OK to insert the data successfully.




Entity Framework 6.1-model First

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.