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