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 ...
随机推荐
- Sphinx 安装与使用
Sphinx 优点 高速索引(接近10M/S) 高速搜索(2-4G文本搜索耗时不到0.1秒) 高可用性(单CPU支持100GB文本,100M文档) 提供相关性排名.分布式搜索.文档摘要(高亮显示) S ...
- Adjacent Bit Counts(01组合数)
Adjacent Bit Counts 4557 Adjacent Bit CountsFor a string of n bits x 1 , x 2 , x 3 ,..., x n , the a ...
- 机器学习中的算法(2)-支持向量机(SVM)基础
版权声明:本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gma ...
- week06 12 我们准备数据 前端调用rpc 前后端联调一下
用postman发送请求 出现一个问题 我在return结果前 要将数据转换成字典 所以我们用json.dumps()后再json.load()回来 这样就避免了这个问题 因为数据结构的数据 比如li ...
- 记账本,C,Github,结果
- 了解各种不同意义上的new
问题1:请说明new operator 和 operator new的差异? 1.new operator : 一般我们写代码的时候,例如:String *p = new String(&quo ...
- 大数据入门到精通11-spark dataframe 基础操作
// dataframe is the topic 一.获得基础数据.先通过rdd的方式获得数据 val ny= sc.textFile("data/new_york/")val ...
- 556. Next Greater Element III下一个更大的数字
[抄题]: Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exac ...
- 8. String to Integer (atoi) 字符串转成整数
[抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...
- 获取CNVD的cookie
def getCookie(self): #获取cookie spiderFirefox = webdriver.Firefox() spiderFirefox.get("http://ww ...