Code First, Database First, Same Time difference, databasesame

Source: Internet
Author: User

Code First, Database First, Same Time difference, databasesame

 

Code First, Database First, and Same Time are the three models used in asp.net mvc that I understand to correspond to databases. They must be incomplete and narrow in understanding, in the future, I will modify this post as I understand it. These three methods are used in www.helpqy.com. Here is my summary:

 

[Code First]

First, write the model class, and then use EF to automatically generate the database and corresponding tables. Code First is actually used when a template page is automatically created using MVC. When you open the new project, you can see that there is no database. When you register a new account on the running page, the system automatically. configuration in config, connection strings set in ApplicationDbContext, and model classes to generate databases and tables. As for how to generate the database, in my other post, "Change SQL SERVER database to MySql" has specific implementation code. In fact, the database is created through the database Initilizer class.

The database created in this way can be synchronized to the database through the "Package Manager Console" of VS after the model class members are modified, you only need to use the Enable-Migrations, Add-Migrations "XXX" and Update-Database steps in data migration to synchronize the Database and model classes.

 

[Database First]

Create a database and a table, and then create a new ADO.. NET object data model file edmx. According to the guidance of VS, select a table of the created database as the data source, and the model class will be automatically created.

 

[Same Time]

Same Time is a word I come up with. It is very unprofessional and a summary of myself. That is, on the Code side, the ApplicationUser in the model has been created. To add other classes and create corresponding tables in an existing database, pay attention to the following points:

(1) Add [Table ('"xxxx Table name")] at the top of the class to associate the class with the Table.

(2) Add the [Key] Attribute before the primary Key field.

(3) If multiple primary keys exist in the table, you must add the [Column (Order = XX Column number)] attribute in addition to the [Key] attribute to the primary Key, to indicate the order of different primary keys. Otherwise, the error "cannot determine the primary key order of the combination" will occur.

(4) Newly Added classes can be created without being placed in ApplicationDbContext. You can create DataContext to establish the Context model and then access it through linq to SQL. However, during actual deployment, in this way, an access conflict error occurs. Therefore, we recommend that you define the DbSet of the new model class in ApplicationDbContext, and then access it through the unified entry ApplicationDbContext.

 


What are the differences between Entity framework database-first, code-first, and model-first?

Database-first (Database first): starts from a Database, and then the Entity Framework generates the corresponding code.
Model-first: starts with a visual Model, and then the Entity Framework generates databases and code.
Code-first: starts from the Code, and then the Entity Framework generates a database

Relationship between Entity Framework and code first

CodeFirst is a technical method of EntityFramework, because the traditional programming method is to first establish a database, then model the application based on the database model, and then develop. CodeFirst is literally the code first, first, create the object structure to be mapped to the database in the program, and then the EntityFramework can generate the corresponding database according to the object structure.

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.