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 一些方法函数

    转Python学习笔记十一:列表(3)--列表的一些方法:http://www.cnblogs.com/dabiao/archive/2010/03/12/1683942.html python中的e ...

  2. java 集合是否有序

    参考:https://www.cnblogs.com/hoobey/p/5914226.html

  3. LevelDB源码分析-Get

    Get LevelDB提供了Get接口用于给定key的查找: Status DBImpl::Get(const ReadOptions &options, const Slice &k ...

  4. 28.Hibernate-HQL查询.md

    目录 1.概述 2.HQL实例 3.Criteria 查询 4.SQL本地查询 5.分页 1.概述 1)Get/load主键查询 2)对象导航查询 3)HQL查询, Hibernate Query l ...

  5. type的解释

    在jquery-19.1.1源码中,type,检查对象的类型是:Boolean/Number/String/Function/Array/Date/RegExp/Object/Error中的一种,返回 ...

  6. 数据导入Excel时,出现ole error 800AC472这个错误,怎么解决。

    我也出现过这个问题 在生成报表的时候不要动EXCEL中的任何单元格 让它完成保存就可以了 或者是把office 2003 删除下载一个office 2000就可以解决 据说是版本兼容的问题 不是高手 ...

  7. Codeforces Round #500 (Div. 2) [based on EJOI]

    Codeforces Round #500 (Div. 2) [based on EJOI] https://codeforces.com/contest/1013 A #include<bit ...

  8. 算法练习LeetCode初级算法之树

    二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...

  9. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

  10. EntityFramework 基础提供程序在 Open 上失败

    最近项目开始上线,所以抽时间学习了一下EF.虽然项目中一直在用,但是因为一些原因,一直是知其然不知其所以然,紧紧只限于会用而已.这两天自己搭建了一个MVC的EF框架,虽然也有参考网上各种资料,但是依然 ...