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. tomcat 8 在线管理admin配置

    在tomcat8下,更加注重安全性.如果要使用在管理控制台部署应用,需要修改更多的配置. 在$tomcat_base$/webapps/manager/META-INF/context.xml中 添加 ...

  2. MySQL索引原理及优化

    一.各种数据结构介绍 这一小节结合哈希表.完全平衡二叉树.B树以及B+树的优缺点来介绍为什么选择B+树. 假如有这么一张表(表名:sanguo): (1)Hash索引 对name字段建立哈希索引: 根 ...

  3. 自然语言处理的CNN模型中几种常见的池化方法

    自然语言处理的CNN模型中几种常见的池化方法 本文是在[1]的基础上进行的二次归纳. 0x00 池化(pooling)的作用   首先,回顾一下NLP中基本的CNN模型的卷积和池化的大致原理[2].f ...

  4. MFC笔记3

    1. C6有默认的提示代码功能,但是其默认的快捷键是Ctrl + Space,这一般情况下是切换输入法快捷键,所以,只需重新设置一下快捷键就可以实现提示代码功能,具体设置位置如下: 工具(T) -&g ...

  5. JAVA类与类之间的全部关系简述+代码详解

    本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...

  6. mysql学习笔记--数据完整性

    一.数据完整性包括: 1. 域完整性 2. 实体完整性 3. 引用完整性 二.保证实体完整性 1. 主键约束 2. 唯一约束 3. 自动增长列 三.保证域完整性 1. 数据类型约束 2. 非空约束 3 ...

  7. Nginx(二)

    利用include功能优化nginx的配置文件 [root@lnmp conf]# cat nginx.conf worker_processes 1; events {     worker_con ...

  8. django mysql 数据库配置

    在settings.py中保存了数据库的连接配置信息,Django默认初始配置使用sqlite数据库. DATABASES = { 'default': { 'ENGINE': 'django.db. ...

  9. vue入门一

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. tp3.2单函数总结

    A($name,$layer='',$level=0) // 实例化多层控制器 格式:[资源://][模块/]控制器         B($name, $tag='',&$params=NUL ...