是否可以从实体中持久保存ComplexTypes集合? [英] Is it possible to persist a collection of ComplexTypes from an Entity?
问题描述
是否可以从实体中保留一组ComplexType?
Is it possible to persist a collection of ComplexTypes from an Entity?
推荐答案
否。将ComplexTypes视为属性组是最容易的。因此,不要说客户有地址第1行属性,地址第2行属性,等等。你可以说客户有一个地址属性,一个地址包含
的第1行,第2行等。当你将它映射到数据库时,所有地址的属性都会被直接注入到客户表格。
No. It's easiest to think of ComplexTypes as property groups. So instead of saying that a Customer has an address line 1 property, address line 2 property, etc. you can just say that a customer has an address property and that an address consists of a line 1, line 2, etc. When you map that to a database, all of the Address's properties get injected directly into the Customer table.
然而,您可以创建一个单独的实体,让您完成您想要做的事情:
You could, however, create a seperate entity that let's you acomplish what you are trying to do:
class Customer
{
public int Id { get; set; }
public virtual ICollection<CustomerAddress> Addresses { get; set; }
}
class CustomerAddress
{
public int Id { get; set; }
public virtual Address Address { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
[ComplexType]
class Address
{
public string Line1 { get; set; }
public string Line2 { get; set; }
// etc.
}
这篇关于是否可以从实体中持久保存ComplexTypes集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!