Entityframework learning notes -- 005-provide a correct explanation for code first, entitycodefirst

Source: Internet
Author: User

Entityframework learning notes -- 005-provide a correct explanation for code first, entitycodefirst

In Microsoft's official introduction to ef7, ef7 will discard database first and model first, and only retain the use of code first. This has aroused a lot of concerns and concerns come from the incorrect understanding of code first. Because many people think that code first is the third method that distinguishes database first from model first. In fact, this is a wrong understanding. In fact, code first is a solution that replaces the first two methods. In other words,Code First is not the third method relative to Database First and Model First, but a solution that can replace the EDMX file format.Technically, Code First supports both Database First and Model First. This is indeed confusing. We have taken the wrong name. It may be called "code-base modeling" to be clearer. Here is an introduction to ef7: https://msdn.microsoft.com/zh-cn/magazine/dn890367.aspx

1. First install entityframework for your project, right-click your project, and add two object class files. The class file information is as follows:

 1 public class Class 2     { 3         public int Id { get; set; } 4         public string Name { get; set; } 5         public virtual List<Student> Students { get; set; } 6         public Class() 7         { 8             Students = new List<Student>(); 9         }10     }
 1 public class Student 2     { 3         //[Key] 4         public int Id { get; set; } 5         public string Name { get; set; } 6         public int ClassId { get; set; } 7  8         //[ForeignKey("ClassId")] 9         public virtual Class Class { get; set; }10     }

1.2 Add a class inherited from DbContext. The DbContext class is the core of ef data access. The class information is as follows:

1 public class MyEfRecipesContext: DbContext 2 {3 public MyEfRecipesContext () 4 {5 // Database. setInitializer (new CreateDatabaseIfNotExists <MyEfRecipesContext> (); 6} 7 public DbSet <Student> Students {get; set;} 8 public DbSet <Class> Classs {get; set ;} 9 10 protected override void OnModelCreating (DbModelBuilder modelBuilder) 11 {12 // here you can modify the configuration before model creation 13} 14}

The annotation code in can be omitted, because the default configuration of ef is like this. Of course, you can manually change the configuration information. In ef, there is a principle that "conventions are greater than configurations". Conventions are actually default configurations, which means the conventions in ef are better not to modify them actively.. Below are some default ef conventions:

(1) database ing: by default, Code First creates a database with the same full name as the DbContext subclass in the local SQL Expression Database. The full name indicates the namespace plus the class name.

(2) Table ing: by default, Code First creates a data table based on the plural type name. For example, the table name corresponding to the Student class is Students.

(3) column ing: by default, Code First will create a column based on the attribute name in the class. It also has the default data type ing habit. int will be mapped to interger, string is mapped to nvarchar (max), and decimal is mapped to decimal ). The following describes how to change the column name, type, and other features.

(4) primary key ing: by default, Code First finds an int type attribute named Id or type name + Id in the class attribute as the primary key and is an auto-increment field. These can also be changed.

1.3 Add a configuration file to App. config. If it is a Web program configuration file, it is Web. config. Do not introduce this. The connection string information is as follows:

  <connectionStrings>    <add name="MyEfRecipesContext" connectionString="Data Source=.;Initial Catalog=myefrecipes;User Id=sa;Password=renjing2000;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />  </connectionStrings>

2. sample code. Add and query. The output is shown in Figure 5-1:

1 using (MyEfRecipesContext db = new MyEfRecipesContext () 2 {3 Class c1 = new Class () {Name = "class1"}; 4 Student s1 = new Student () {Name = "Example 1", Class = c1}; 5 Student s2 = new Student () {Name = "Example 2", Class = c1}; 6 7 db. students. add (s1); 8 db. students. add (s2); 9 int I = db. saveChanges (); 10 11 Console. writeLine ("id \ t name \ t class"); 12 foreach (var item in db. students) 13 {14 Console. writeLine ("{0} \ t {1} \ t {2}", item. id, item. name, item. class. name); 15} 16}

Figure 5-1

3. After running the program, the generated table structure is 5-2. We can see that the Students table monthly Classes table generates a "one-to-many" relationship, just as we expected. "_ MigrationHistory" is a table automatically generated by ef. It is mainly used for some configuration information of ef and does not need to be concerned.

 

Figure 5-2

 

Related Article

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.