Entity Framework Code First learning diary (1), entityframework

Source: Internet
Author: User
Tags in domain

Entity Framework Code First learning diary (1), entityframework

I have been learning Entity Framework Code First in recent days. I plan to share a series of study notes. Today is the First part:

Why use Code First:

In recent years, with the promotion of domain driven design, more and more questions have been raised about how to first build a database and then write code. Because this development method is difficult to adapt to changes in the business logic in the domain, it needs to change the database first when the domain changes, and then change the business logic and entity code, it has a long development cycle and is not conducive to unit testing. Therefore, the Persistence Ignorance principle and POCO (Plain Old CLR Object) are promoted along with domain driven design ). persistence Ignorance principle is to completely isolate entities, value objects, services, and data storage functions in domain driven design so that they do not mix any Code related to data storage. In the subsequent instances, you can see how to implement this through Code First.

The so-called Code First is to First write the Code of the business logic to implement the entity, value type, and service in domain driven design, then, map the default habits or configurations of Code First to the database. In this way, we can put all our energy into the design and implementation of the business logic, and after implementing the business logic, use some Mock tools to test the business logic Code separately. On the other hand, when we perform proof-of-concept on the business logic, prototype can be presented to the customer through several simple interfaces. By using Code First, we can quickly change the business logic and build a prototype. So Code First is very suitable for new projects that use domain driven design.

Regarding the benefits of using Code First and its usage, I believe that we have already learned through the previous introduction and let us go back to the Code, use the simplest instance program to see how Code First maps databases according to its conventions.

Code First follows the Convention over Configuration principle, that is, If you do not configure the ing of various objects in the field, code First maps domain objects to databases according to its default habits.

 

The example I used in this series of diaries is a simple order management system for new employees in the company to train and use, including customers, orders, order entries, products, product catalog and other entities and value objects.

 

Let's first consider the simplest product directory. Assume that our product directory is a value object that contains the following attributes:

Public class ProductCatalog
{
Public int ProductCatalogId {get; set ;}
Public string CatalogName {get; set ;}
Public string Manufactory {get; set ;}
Public decimal ListPrice {get; set ;}
Public decimal NetPrice {get; set ;}
}

 

How can we let Entity Framework know your defined value object and map it to a table in the database? You need to define a subclass that inherits the DbContext class, and then add a generic DbSet attribute to this subclass. The type parameter is your custom ProductCatalog class.

Public class OrderSystemContext: DbContext
{
Public DbSet <ProductCatalog> ProductCatalogs {get; set ;}
}

Then you can use the OrderSystemContext class you defined to perform data operations.

Var context = new OrderSystemContext ();

Var catalogs = context. Catalogs. Where (c => c. ProductCatalogId = 1 );

Now let's introduce the ing habits in Entity Framework Code First.

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. Of course, we will introduce how to configure it later.

2. Table ing: by default, Code First creates a data table based on the plural type name. For example, the name of the table corresponding to the ProductCatalog class is ProductCatalogs. Later, we will introduce how to change the default table name.

3. column ing: by default, Code First creates a column based on the attribute names in the class. It also has the default data type ing habits. int maps to interger, and string maps to nvarchar (max ), 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.

After this program is executed, a class named xxx. OrderSystemContext is created in the default SQL Expression and a table named ProductCatalogs is created. The table structure is as follows:

 

Of course, since we use domain driven design, we should design our classes according to the actual business conditions in our field, so that our classes cannot fully abide by the habit of Code First, the next learning diary will mainly introduce how to map various classes and classes in our field to the database through the Code First configuration function.

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.