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. lambda表达式,filter,map,reduce,curry,打包与解包和

    当然是函数式那一套黑魔法啦,且听我细细道来. lambda表达式 也就是匿名函数. 用法:lambda 参数列表 : 返回值 例: +1函数 f=lambda x:x+1 max函数(条件语句的写法如 ...

  2. upcast 做了什么操作

    把子类中仅仅继承而来的成员,赋值给父类. 但是,不会改变虚表!因为这个obj的类型没变. #include <stdio.h> using namespace std; class A{ ...

  3. ORM的相关操作

    http://www.cnblogs.com/liwenzhou/p/8660826.html

  4. Aplication的意义和生命周期,与Context的关系,以及关于Aplication和Context相关问题的记录和解决办法

    Context详解地址链接: http://blog.csdn.net/qinjuning/article/details/7310620 Application是一个应用中有且仅有一个的全局共享变量 ...

  5. Nginx虚拟目录设置

    location ~ .*\.html$   匹配所有以.html结尾的链接 --------------------------------------------------------- 关于a ...

  6. 算法题——给定一个数组 arr,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

    参考自:https://blog.csdn.net/qq_38200548/article/details/80688630 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] ...

  7. yum源解释。。。。。

    主要说明下如何配置linux上的本地yum源,主要关于一些原理上的说明. 1.yum是什么,yum源又是什么       在windows上安装一个软件,我们可以通过360管家.因为360管家提供了软 ...

  8. .NET代码混淆——开源.net 混淆器ConfuserEx介绍

    转载:https://blog.csdn.net/xiaoyong_net/article/details/78988264

  9. Windows Server 2012安装密钥

    Windows Server 2012 Standard 密钥:NB4WH-BBBYV-3MPPC-9RCMV-46XCB Windows Server 2012 StandardCore 密钥:NB ...

  10. python list中append()方法和extend()方法区别

    共同点 只能作用于list类型(不能作用于tuple等其他类型) 单参数限制(不支持多参数) 不同点 list.append(object) 向列表中添加一个对象object. 使用append的时候 ...