Entity Framework code first learning diary (5)

Source: Internet
Author: User
Tags in domain

Today, I want to introduce how to use the Advanced Configuration feature of Entity Framework code first to process another important component in domain driven design: value object, which is translated as a value object in Chinese.

A value object is a class without a lifecycle or a unique identifier in the business logic. Which classes are Entity and which classes are value objects are not fixed depends on the specific business logic. For example, if the customer class is the most important information in the CRM system, we need to track its status and manage its lifecycle. However, in other systems, customer information may only represent one name and some other attributes.

The value object cannot be modified because it only represents some temporary Property sets and does not have a life cycle to be maintained.

Our order management system has a customer class. In our previous example, the address attribute of the customer class is a string. Now our business logic has changed. We need to display and store the address information in different categories, and classify the address information into country, province, city, street, street number, and zip code. In our business, the address information is only a set of attributes. It does not need to be tracked, nor does it have a unique identifier in the business. So we define it as a value object:

Public Class address
{
Public String country {Get; set ;}
Public String province {Get; set ;}
Public String city {Get; set ;}
Public String streetaddress {Get; set ;}
Public String zipcode {Get; set ;}
}

To enable Entity Framework code first to recognize value objects by default, our class must have three conditions:

1. The value object class cannot have a primary key.

2. The value object class can only contain. Net basic type attributes.

3. A class using a value object can only contain one instance of a value object and cannot use a set of value objects.

Then we need to change our customer class and use the address value object to replace the previous string.

Public Class Customer
{
Public String idcardnumber {Get; set ;}
Public String customername {Get; set ;}
Public String gender {Get; set ;}
Public Address {Get; set ;}
Public String phonenumber {Get; set ;}
}

If the value object does not meet the three Default Value Object Recognition conditions, we need to declare address as a value object in the onmodelcreating method of the custom dbcontext class.

Modelbuilder. complextype <address> ();

In our unit test, we add a new test method to use the address value object.

[Testmethod]
Public void canaddnewcustomerwithaddress ()
{
Ordersystemcontext unitofwork = new ordersystemcontext ();
Customerrepository repository = new customerrepository (unitofwork );
Customer newcustomer = new customer () {idcardnumber = "120104198106072518", customername = "Alex", Gender = "M", phonenumber = "test "}
Address customeraddress = new address {Country = "China", Province = "Tianjin", city = "Tianjin", streetaddress = "Crown Plaza", zipcode = "300308 "};
Newcustomer. Address = customeraddress;
Repository. addnewcustomer (newcustomer );
Unitofwork. commitchanges ();
}

 

We use the dropcreateorderdatabasewithseedvaluealways that can insert basic data to customize the database initialization class, and re-establish the database before each test method is executed.

[Testinitialize]
Public void initializecustomerrepositorytest ()
{
Database. setinitializer (New dropcreateorderdatabasewithseedvaluealways ());
}

The structure of the customer table after the test program is executed is as follows:

We can see that Entity Framework code first uses the attribute of the address value object as the column of the MERs table by default. By default, the column name is the name of the value object class + "_" + The Name Of The Value Object Property.

We can change the default Processing Method of Entity Framework code first.

We can define a class that inherits from complextypeconfiguration <address>. In this class, we can reload the default Processing Method of the code first value object. We can change the names of those columns and their data types and lengths.

Public class addresscomplextypeconfiguration: complextypeconfiguration <address>
{
Public addresscomplextypeconfiguration ()
{
Property (A => A. Country). hascolumnname ("country"). hasmaxlength (100 );
Property (A => A. Province). hascolumnname ("Province"). hasmaxlength (100 );
Property (A => A. City). hascolumnname ("city"). hasmaxlength (100 );
Property (A => A. streetaddress). hascolumnname ("streetaddress"). hasmaxlength (500 );
Property (A => A. zipcode). hascolumnname ("zipcode"). hasmaxlength (6 );
}
}

Through hascolumnname, we can change the name of the column corresponding to the attribute. Entity Framework code first uses the attribute name in the class as the column name by default.

Run our test method again. The structure of the customer table is as follows:

I will introduce the configuration related to the data table tomorrow. Although I wrote this series of articles mainly to consolidate my experience in learning and using Entity Framework code first, if you think that my expressions are not good or too cool, I hope you can make suggestions. I will try my best to correct them. Thank you.

Wang fatzi's 2012 opinion.

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.