Complex types are classes that map to a subset of columns of a table.They don't contains key. They are the Value Objects. For example, you have a entity named Company:

public class Company
{
public int CompanyId { get; set; } public string CompanyName { get; set; } public string Street { get; set; } public string City { get; set; } public string Zip { get; set; }
}

You can Mondify it like this:

public class Company
{
public Company()
{
this.Address = new Address();
}
public int CompanyId { get; set; } public string CompanyName { get; set; } public Address Address { get; set; }
} public class Address
{
public string Street { get; set; } public string City { get; set; } public string Zip { get; set; }
}

This structure can made the entity's meaning clearer than before. and the class Address can be used as a common value object for many classes. Now we complete all codes:

public class Company
{
public Company()
{
this.Address = new Address();
}
public int CompanyId { get; set; } [MaxLength(100)]
public string CompanyName { get; set; } public Address Address { get; set; }
} public class Person
{
public Person()
{
this.Address = new Address();
}
public int PersonId { get; set; } [MaxLength(50)]
public string PersonName { get; set; } public Address Address { get; set; }
} public class Address
{
[MaxLength(100)]
public string Street { get; set; } [MaxLength(50)]
public string City { get; set; } [MaxLength(10)]
public string Zip { get; set; }
} public class MyContext:DbContext
{
public MyContext() : base("name=Test")
{ } public DbSet<Company> Companies { get; set; } public DbSet<Person> People { get; set; }
}

Then, execute following commands in NuGet command line:

  • Enable-Migrations
  • Add-Migration Init
  • Update-Database

Now, The table people and companies'structures like this:

Let's have a test:

static void Main(string[] args)
{
Address address = new Address
{
City = "BeiJing",
Street = "ChangAn Street",
Zip = "119"
}; Company company = new Company
{
CompanyName = "TianAnMen",
Address = address
}; Person person = new Person
{
PersonName = "伟人",
Address = address
}; using (MyContext db = new MyContext())
{
db.Companies.Add(company);
db.People.Add(person); db.SaveChanges();
}
}

You can provide the configuration for complex types. We can do so by providing a configuration class . The only difference is that we use a different base class, ComplexTypeConfiguration, not EntityTypeConfiguration.

That's all.

Lerning Entity Framework 6 ------ Complex types的更多相关文章

  1. Lerning Entity Framework 6 ------ Defining Relationships

    There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...

  2. Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database

    The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...

  3. Lerning Entity Framework 6 ------ Working with in-memory data

    Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...

  4. Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data

    Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...

  5. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  6. Lerning Entity Framework 6 ------ Introduction to TPH

    Sometimes, you have created two models. They have the same parent class like this: public class Pers ...

  7. Entity Framework Core Query Types

    This feature was added in EF Core 2.1 Query types are non-entity types (classes) that form part of t ...

  8. Lerning Entity Framework 6 ------ Using a commandInterceptor

    Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...

  9. Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql

    Create a new project named MySqlTest Install following packages by right-clicking on the References ...

随机推荐

  1. Android Studio Intent使用(显式、隐式)

    https://blog.csdn.net/u012005313/article/details/47006689 使用Intent能够使程序在不同活动中跳转,意及能够使用不同界面.Intent用法分 ...

  2. http://www.36dsj.com/archives/46131

    https://docs.growingio.com/Developer%20Document.html https://youdata.163.com/dash/39706/editor?pid=7 ...

  3. jQuery 新添加元素事件绑定无效

    jQuery中事件绑定,大多使用on就足够了. 但是对于新添加的元素 on 的绑定事件 会不起作用. 因为 append 中的 节点是在整个文档加载之后才添加的,页面并不会为未来的元素初始化添加点击事 ...

  4. AltiumDesigner印制导线的走向及形状

    印制导线的走向及形状.在PCB布线时,相邻层的走线方向成正交结构,应避免将不同的信号线在相邻走成同一方向,以减少不必要的层间窜扰.当PCB布线受到结构限制(如某种背板)难以避免出现平行布线时,特别是当 ...

  5. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

  6. day 32 JavaScript

    1.1. JavaScript介绍 HTML:定义网页的结构 CSS:美化网页 JavaScript:实现用户交互: 1.1.2 JavaScript特点 n  安全性较高 n  跨平台,兼容性好 1 ...

  7. CentOS_mini下安装docker 之 yum mount

    --->linux 终端输出太多前面看不到的解决办法:shift+page up --->mount命令[-参数] [设备名称] [挂载点] mkdir /mnt/CentOS mount ...

  8. windows远程桌面连接时,显示发生身份验证错误,给函数提供的身份无效

    摘自:https://www.landui.com/help/show-7787 初次看到这个错误的时候懵了.访问给的地址一看,发现大概意思是不安全了,微软要更新一下 凭据安全支持提供程序协议 (Cr ...

  9. linux学习第七天 (Linux就该这么学)

    今天讲了chmod (权限 设置)和 chown(属性 设置),特殊权限:SUID u+s 数字法是4  x=s  - = S,SGID g+s 数字法是2 x=s -=S,SBIT o+t  x=t ...

  10. windows powershell

    今天用了一条命令: dism /online /add-package /packagepath:c:xxxx.cab