Lerning Entity Framework 6 ------ Complex types
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- 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 ...
- Lerning Entity Framework 6 ------ Using a commandInterceptor
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...
- 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 ...
随机推荐
- Web框架本质及第一个Django实例 Web框架
Web框架本质及第一个Django实例 Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web ...
- 多进程模块 multiprocessing
由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...
- IO密集型和计算密集型
我们常说的多任务或者单任务分为两种: IO密集型的任务 计算密集型的任务 IO密集型的任务或:有阻塞的状态,就是不一直会运行CPU(中间就一个等待状态,就告诉CPU 等待状态,这个就叫IO密集型 ...
- Delphi中Chrome Chromium、Cef3学习笔记(二)
原文 http://blog.csdn.net/xtfnpgy/article/details/46635739 用Tchromium替换webbrowser 用惯了EmbeddedWB,不想 ...
- BP神经网络与Python实现
人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...
- Docker 清理命令
原文地址http://www.runoob.com/w3cnote/docker-clear-command.html,这里仅作为记录,便于以后查阅 查看正在运行的容器(Container) dock ...
- C# 反编译
今儿也是运气背,不知怎么的,一脚就把电脑踢关机了(其实就轻轻碰到了一下主机),我去,写了一早上的代码,尼玛就不见,不见就算了,其实是保存了的,主要是文件还损坏了,尼玛,那心情!!! 然后就想着恢复,下 ...
- javaMail实现收发邮件(一)
电子邮件的传输过程 电子邮件系统采用客户/服务器模式.电子邮件传送需要用到以下3个重要模块:MUA(Mail User Agent,邮件用户代理):用户通过它与电子邮件服务器打交道.MUA实际上就是邮 ...
- netty(一) netty有哪几部分构成
netty netty是一个支持高并发的非阻塞I/O框架. Channel Channel 是 NIO 基本的结构.它代表了一个用于连接到实体如硬件设备.文件.网络套接字或程序组件,能够执行一个或多个 ...
- wampserver安装
WampServer的下载地址:https://sourceforge.net/projects/wampserver 安装教程:https://jingyan.baidu.com/article/4 ...