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. Python学习笔记---切片 列表 元祖 字典 集合

    列表[1,2,3,2]#[] 元祖(1,2,3,2)#() 字典{1:2,3:2}#{} 集合{1,2,3,2}#{} 1,集合与列表的区别,集合里不能有重复元素 2.字典与集合的区别,都是用花括号表 ...

  2. vim移动一行或一段代码

    nmap <M-j> mz:m+<cr>`z nmap <M-k> mz:m-2<cr>`z vmap <M-j> :m'>+< ...

  3. JavaScript: For , For/in , For/of

    For: define: The for statement can customize how many times you want to execute code Grammar: for (c ...

  4. 使用List需要注意的点

    目录 1. 概述 2. Arrays.asList(); 2-1. 产生不可操作的集合 2-2. 需要传入对象数组而不是基本类型数组 3. arrayList.subList(); 3-1. subL ...

  5. git配置正确且权限已开但是pull或push提示无权限

    因为之前提示输入用户名和密码时输入错误,之后就一直权限认证失败.这种情况下在git bash中输入: git config --system --unset credential.helper 就会重 ...

  6. python--第十四天总结(js)

    选择器允许您对元素组或单个元素进行操作. jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元 ...

  7. 【VBA】杨辉三角

    Private Sub Workbook_Open() Dim loopA As Integer Dim loopB As Integer Dim loopNum As Integer Dim top ...

  8. cisco PBR

    access-list 2000 permit ip 10.11.50.0 0.0.0.255 anyaccess-list 2001 permit ip 10.11.50.0 0.0.0.255 1 ...

  9. 关于弹性布局的 flex-grow的用法和flex-shrink的用法

    1.首先 flex-grow设置在子项目上 2.flex-grow默认值为0,如果为值1的时候就会撑满 3.flex-grow还可以给其中的一个子元素单独设置,设置为2,其它的则为1或者2都可以,具体 ...

  10. c#子线程线程中操作窗体更新的报错

    用 在执行上传时,由于操作较长窗体界面卡住,于是用task解决 Task t1 = new Task(manage.UploadData); t1.Start(); 结果不卡了,程序也传完了,运行到更 ...