I would like to share with you how Entity Framework connects to an existing database using Code First.

Source: Internet
Author: User

The title may have to be criticized and pointed out: Code First advocates the use of object-oriented modeling and then the automatic generation of databases by EF (the advantages and disadvantages are not discussed First, I always look forward to this design method). If you first create a database model and then generate an entity model, you can use the EF template of VS2010 to select all the tables, which is more convenient to generate. What's more ...... However, in addition to the best practices, there are also many scenarios that really need to be done. We will not discuss these here. Back to the question, we should also have a better understanding of the Code First method.

 

(This article assumes that you already know EF and Code First. Of course, you can also find other High Hand blogs in the blog)

 

By default, after you implement your own database context and perform CRUD operations on the context, EF automatically generates a database (or connects to an existing database) according to the context definition ). The most easily overlooked question is: how is the name of the generated database determined? How is the table name determined? These two problems directly determine whether to generate a database or connect to the original database or even throw an exception. Back to our topic, we aim to connect to the existing database, so we can analyze its principles from this point.

First, let's talk about the database name. When the database context object is instantiated using the default constructor (No parameter construction), the database name will be the fully qualified name of the custom database context (custom DbContext, that is, "namespace. context class name ". For example:

 

The database name is "TopwayAD. Dtv. Data. SqdlDB ". I believe that more than 80% of the database design comrades will not name the database like this, most of them have generated a new database. How can we "control" The Database Name and connect it to the existing database? The answer is simple. It is to use the constructor with parameters in the database context. This method also enables ADO. NET connection string can be reused in the most direct way, of course you do not like Code First, it is another matter, huh, huh. The constructor signature is as follows:

 

Public SqdlDB (string connectionString): base (connectionString)

 

When the constructor is called to instantiate the database context, the database name will be taken from the definition part of the database name in the connection string. For example:

In addition, the table name will be mapped in the plural form of the entity type name of the DbSet generic attribute in the Custom DbContext. If no value is found, an exception will be thrown !! Note: It is the type name of the object class, not the attribute name of the DbSet generic type in the context! If the Table name of an existing database is not in the complex form of an object class, you can add the Table feature on the object class to specify which Table to map, that is, [Table ("Table name")]. For example:

By default, the database needs to map the "Areas" table. If the table is not in the Context database, an exception is thrown.

 

Finally, the ing between fields and object attributes in the database table is supplemented. By default, the ing between the table fields and object attribute names is the same and case-insensitive. When an object has a base class and defines the property ing (for example, the Id attribute) in the base class, the data table fields must be moved to the base class, you need to change the corresponding column of the database table to the same name as the attribute of the base class. (I have not studied any other solutions here. If you have any questions, please let us know.) from another perspective, use the ID column as an example, we recommend that you define the primary key column as "ID" instead of using the "entity ID + ID suffix" naming method during data modeling. Of course, the foreign key column name must be named by "entity ID + ID suffix.

For other default rules, you can view the contract http://blog.joycode.com/saucer/archives/2010/10/08/116098.joy in Code First

For more EF documentation support, see MSDN: Entity Framework 4.1 http://msdn.microsoft.com/en-us/library/gg696172 (v = VS.103). aspx

 

The time relationship is written here, because my understanding of the EF and Code First methods is only superficial, please criticize and correct High Hand.

Source: http://www.cnblogs.com/Ryu666/archive/2011/09/18/EFConnectTOExistDB.html

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.