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 ...
随机推荐
- leetcode279
动态规划 public class Solution { public int NumSquares(int n) { var list = new List<int>(); list.A ...
- .NET MVC 控制器和行为
行为就是可访问方法(public) 行为返回类型必须是 ActionResult 或者其派生类,基本上返回类型为以下四种之一 View(视图路径) Json(对象或者对象集合) Content(字符串 ...
- java.util.Stack类中的peek()方法
java.util.stack类中常用的几个方法:isEmpty(),add(),remove(),contains()等各种方法都不难,但需要注意的是peek()这个方法. peek()查看栈顶的对 ...
- 最流行的Python编辑器/IDEs你认识吗?
来源商业新知网,原标题:来!带你认识几种最流行的Python编辑器/IDEs(附链接) 大数据文摘授权转载自数据派THU 作者:By Gregory Piatetsky 格雷戈里·皮亚特斯基,KDnu ...
- python 内置元祖子类
a = (zhangsan,20,nv,123@163.com) 输出姓名 a[0] 输出年龄 a[1] 输出性别 a[2] ..... 这样写可读性非常低 使用 内置元祖子类 from collec ...
- python 文件读写方式
一.普通文件读写方式 1.读取文件信息: with open('/path/to/file', 'r') as f: content = f.read() 2.写入文件中: with open('/U ...
- java通过年月得到该月每一天的日期
public static List<String> getDayByMonth(int yearParam,int monthParam){ List<String> lis ...
- PHP请求ws出现的问题
在SOAPUI中的请求如下: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/&qu ...
- eclipse 如何安装freemaker ftl 插件
借鉴原链接 https://blog.csdn.net/lsygood/article/details/80565933 在线安装的方法是:Help –> Install New Softw ...
- 31.Mysql复制
31.Mysql复制复制是指将主数据库的DDL和DML操作通过二进制日志传到从数据库上,然后在从数据库上对重做日志,从而使从库与主库保持同步.Mysql支持一台主库同时向多台从库复制,从库也可以作为其 ...