Ef poco application Series 3-complex types

Source: Internet
Author: User

In. in NET development, versions earlier than EF4 and linq to SQL do not support the complex data type. EF4 finally supports the complex data, this means that Microsoft's EF framework has taken another big step towards field-driven development.

A complex data type is actually a POCO class. When we wanted to design a user information class during development, the class we wrote had to include the following information:

Public class UserInfo {

Public int UID {GET; SET };

Public string Identity {get; set };

Public string FirstName {get; set };

Public string LastName {get; set };

}

Our names generally consist of FirstName and LastName. Now let's change it to a complex data type to see how it is implemented.

Public class UserInfo {

Public int UID {GET; SET ;};

Public string Identity {get; set };

Public Name name {get; set ;};

}

Public class Name {

Public string FirstName {get; set };

Public string LastName {get; set };

}

Continue with the Northwind in the previous article. We will continue to expand the product class and add a complex inventory detail "InventoryDetail" to the class ".

1. Open "Northwind. edmx" and click "product-" Add.

2. Open the Model Browser and add scalar attributes for complex types, such:

Add the following attributes to the detail class:

Public Int16 UnitsInStock {get; set ;}
Public Int16 UnitsOnOrder {get; set ;}
Public Int16 ReorderLevel {get; set ;}
3. Add the InventoryDetail. cs file to the NorthwindModel project. The Code is as follows;

View Code

1     public class InventoryDetail 
2 {
3 public Int16 UnitsInStock { get; set; }
4 public Int16 UnitsOnOrder { get; set; }
5 public Int16 ReorderLevel { get; set; }
6 }

4. Change the product. cs code and add the complex type "InventoryDetail" as follows:

View Code

 1     public class Product
2 {
3 public int ProductID { get; set; }
4 public string ProductName { get; set; }
5 public int SupplierID { get; set; }
6 public string QuantityPerUnit { get; set; }
7 public decimal UnitPrice { get; set; }
8 public InventoryDetail InventoryDetail { get; set; }
9 public bool Discontinued { get; set; }
10 public Category Category { get; set; }
11 }

Pay attention to the red part.

Now you can use the following code in the test to find inventory details:

Var outOfStockProducts = from c in context. Products
Where c. InventoryDetail. UnitsInStock = 0
Select c;

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.