EntityFramework Study notes--005-give code first a correct explanation

Source: Internet
Author: User
Tags connectionstrings

In an introduction to Microsoft's official EF7, EF7 will abandon database first and model first, preserving only the use of code first. This has caused a lot of concern and concern stems from the erroneous understanding of code first. Because many people think that code first is the third way to differentiate between database and model, it is a false understanding. In fact, code first is a solution to replace the previous two methods. In other words,Code First is not the third way to compare Database and model, but rather a scheme that can replace the EDMX file format. Conceptually, Code first supports both the database first and model first working methods. It's really confusing, we've got the wrong name. Perhaps calling it "code-based Modeling (Code-base modeling)" would be clearer. Here is an introduction to EF7 document: https://msdn.microsoft.com/zh-cn/magazine/dn890367.aspx

1. First install the EntityFramework for your project, then right-click on your project, add two entity class files, and the class file information is as follows:

1  Public classClass2     {3          Public intId {Get;Set; }4          Public stringName {Get;Set; }5          Public VirtualList<student> Students {Get;Set; }6          PublicClass ()7         {8Students =NewList<student>();9         }Ten}
1  Public classStudent2     {3         //[Key]4          Public intId {Get;Set; }5          Public stringName {Get;Set; }6          Public intClassId {Get;Set; }7 8         //[ForeignKey ("ClassId")]9          Public VirtualClass Class {Get;Set; }Ten}

1.2 When adding a class that inherits from DbContext, the DbContext class is the data access core of EF. The class information is as follows:

1  Public classMyefrecipescontext:dbcontext2     {3          PublicMyefrecipescontext ()4         {5             //Database.setinitializer (New createdatabaseifnotexists<myefrecipescontext> ());6         }7          PublicDbset<student> Students {Get;Set; }8          PublicDbset<class> Classs {Get;Set; }9 Ten         protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) One         { A             //This can be used for configuration modifications prior to model creation -         } -}

the comment code in can be omitted, because this is the default configuration for EF. Of course you can change the configuration information manually. In EF There is a principle of "convention greater than configuration", which is actually the default configuration, meaning that the contract in EF is best not to take the initiative to modify it . Here are some of the default conventions for EF:

(1) Database mapping: Code first establishes a database in the local SQL expression database with the same full name as the DbContext subclass, and the full name refers to the namespace plus the class name. Of course, we'll show you how to configure it behind.

(2) Table mapping: Code first by default will be based on the type name complex data table, for example, the ProductCatalog class corresponding table name is called ProductCatalogs. The following describes how to change the default table name.

(3) Column mapping: Code first establishes column by default by the name of the property in the class, and it has the default data type mapping habit, the int is mapped to interger,string as nvarchar (max), and the decimal is mapped to decimal ( 18,2). The back describes how to change the column name, type, and other attributes.

(4) Primary key mapping: Code first takes the property of the class by default to find the attribute of type int with the name ID or type name +id as the primary key, and is the self-increment field. These can be changed as well.

1.3 Add a configuration file in App. config, and if it's a Web program configuration file, then it's a website. The connection string information is as follows:

  <connectionStrings>    <add name="myefrecipescontext" connectionstring=  TheData source=.;i Nitial catalog=myefrecipes; User Id=sa; password=renjing2000; Multipleactiveresultsets=true; " providername="System.Data.SqlClient "/>  </connectionstrings >

2. Sample code, new and query:

1 using(Myefrecipescontext db =NewMyefrecipescontext ())2             {3Class C1 =NewClass () {Name ="Class1" };4Student S1 =NewStudent () {Name ="rj1", Class =C1};5Student s2 =NewStudent () {Name ="rj2", Class =C1};6 7 db. Students.add (S1);8 db. Students.add (S2);9                 inti =db. SaveChanges ();Ten  OneConsole.WriteLine ("id\t name \ t class"); A                 foreach(varIteminchdb. Students) -                 { -Console.WriteLine ("{0}\t{1}\t{2}", item. Id,item. Name,item. Class.name); the                 } -}

EntityFramework Study notes--005-give code first a correct explanation

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.