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 ...
随机推荐
- leetcode85
class Solution { public int maximalRectangle(char[][] matrix) { if(matrix == null || matrix.length = ...
- oracle数据链接
using System; using System.Collections.Generic; using System.Data; using System.Data.OracleClient; u ...
- 【FZSZ2017暑假提高组Day1】最大矩形
[问题描述] 现在有n个宽度为1的矩形按如下图(左边的)所示的方式排在了一起: 用肉眼容易看出,在左图内部面积最大的矩形如右图绿色部分所标注. 现在我们考虑将其中一些宽度为1的矩形取出,按照原顺序再次 ...
- POI richText和html的转换案例
private XSSFRichTextString parseHtmlStrToRichText(String htmlStr) { Document document = parseHtmlStr ...
- go相关知识点
后续开发go相关, 环境搭建 go env //查看环境所有 go只有三种引用类型 slice(切片). map(字典). channel(管道): go的类型的浅记忆 4仲类型bool,字符串,数字 ...
- [Ting's笔记Day6]活用套件carrierwave gem:(1)在Rails实现图片上传功能
carrierwave是一款经典的图片上传套件,本篇的目标是先在本地端(development)的rails项目试成功gem. (预计中集的进度会练习怎么利用Amazone S3架设图片上传Host, ...
- 《CSAPP》符号解析
符号解析 链接器解析符号引用的方法是将每个引用与它输入的可重定位目标文件的符号表中的一个确定的符号定义联系起来.编译器只允许每个模块中每个本地符号只有一个定义. 对于全局符号,当编译器遇到一个不是在当 ...
- 算法练习LeetCode初级算法之排序和搜索
合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...