I have tried to read the msdn article on complex types. But it does not explain when to use it. Also there is not a comprehensive explanation on the web on complex types and when to use them.
我试图阅读有关复杂类型的msdn文章。但它没有解释何时使用它。此外,在网络上没有关于复杂类型以及何时使用它们的全面解释。
2 个解决方案
#1
26
The lengthy explanation is in the MSDN article you linked... so you basically want an easy explanation:
冗长的解释是在你链接的MSDN文章中...所以你基本上想要一个简单的解释:
A complex type is a set of properties that exist in its own object for C#, but are mapped to columns on an already existing table (the one for the entity that contains it), instead of having its own table (which would need a key, etc.).
复杂类型是一组属性,它们存在于C#自己的对象中,但是映射到现有表(包含它的实体的表)上的列,而不是拥有自己的表(需要一个键)等)。
So imagine you want this table on the database:
所以想象一下你想在数据库上使用这个表:
Orders
----------
Id (bigint)
Name (varchar)
Street (varchar)
Region (varchar)
Country (varchar)
But want this structure in the C# entities:
但是想在C#实体中使用这个结构:
class Order
{
long Id;
string Name;
struct Address
{
string Street;
string Region;
string Country;
}
}
So there Address
would be a complex type: it would not exist on its own (there wouldn't be Addresses
table) on the database... it would only exist as a set of columns on the Orders
table.
因此,Address将是一个复杂的类型:它不会自己存在(在数据库上没有Addresses表)...它只会作为Orders表上的一组列存在。
As noted by @HenkHolterman in the comments, the value of having complex types is having a single C# entity which can be used as a value for other containing entities (in my example, you could have an Address
in a Supplier
entity, for example, but it will just be mapped as a set of columns in the Suppliers
table). It makes it easy to work with the values in the complex type.
正如@HenkHolterman在评论中所指出的,具有复杂类型的价值是拥有一个C#实体,可以将其用作其他包含实体的值(在我的示例中,您可以在供应商实体中拥有一个地址,例如,但它只会被映射为Suppliers表中的一组列。它使得处理复杂类型中的值变得容易。
The disadvantage is precisely that one: you may have to repeat the complex type values many times in the database if it happens that a same Address
(or whatever other type you use) can be shared among different entities.
缺点恰恰在于:如果碰巧在不同实体之间共享相同的地址(或您使用的任何其他类型),则可能必须在数据库中多次重复复杂类型值。
Whether you choose to work with complex types or separate entities is up to you and your design.
无论您选择使用复杂类型还是单独的实体,都取决于您和您的设计。
#2
7
Consider this ContactDetails
class for example:
考虑这个ContactDetails类,例如:
public class ContactDetails
{
public string HomePhone { get; set; }
public string MobilePhone { get; set; }
public string FaxNumber { get; set; }
}
By default, EF will treat ContactDetails
as an Entity. That means that if (for example) you're having a Person
class with a navigation-property of ContactDetails
type, EF will map the Person.ContactDetails
relationship to a different table (because Entity is something that is having an identity of its own, hence other entities may refer to it - and that would require a different table in relational terms).
默认情况下,EF会将ContactDetails视为实体。这意味着如果(例如)你有一个带有ContactDetails类型的导航属性的Person类,EF会将Person.ContactDetails关系映射到另一个表(因为Entity是具有自己身份的东西,因此,其他实体可能会引用它 - 这需要在关系术语中使用不同的表格。
By denoting ContactDetails
as a Complex Type instead, EF will no longer treat it as an entity that requires a relationship and instead map it to the same table of the parent (containing) entity (Person
in my example), effectively making it a Value Object.
通过将ContactDetails表示为复杂类型,EF将不再将其视为需要关系的实体,而是将其映射到父(包含)实体(我的示例中为Person)的同一个表中,从而有效地使其成为值对象。
#1
26
The lengthy explanation is in the MSDN article you linked... so you basically want an easy explanation:
冗长的解释是在你链接的MSDN文章中...所以你基本上想要一个简单的解释:
A complex type is a set of properties that exist in its own object for C#, but are mapped to columns on an already existing table (the one for the entity that contains it), instead of having its own table (which would need a key, etc.).
复杂类型是一组属性,它们存在于C#自己的对象中,但是映射到现有表(包含它的实体的表)上的列,而不是拥有自己的表(需要一个键)等)。
So imagine you want this table on the database:
所以想象一下你想在数据库上使用这个表:
Orders
----------
Id (bigint)
Name (varchar)
Street (varchar)
Region (varchar)
Country (varchar)
But want this structure in the C# entities:
但是想在C#实体中使用这个结构:
class Order
{
long Id;
string Name;
struct Address
{
string Street;
string Region;
string Country;
}
}
So there Address
would be a complex type: it would not exist on its own (there wouldn't be Addresses
table) on the database... it would only exist as a set of columns on the Orders
table.
因此,Address将是一个复杂的类型:它不会自己存在(在数据库上没有Addresses表)...它只会作为Orders表上的一组列存在。
As noted by @HenkHolterman in the comments, the value of having complex types is having a single C# entity which can be used as a value for other containing entities (in my example, you could have an Address
in a Supplier
entity, for example, but it will just be mapped as a set of columns in the Suppliers
table). It makes it easy to work with the values in the complex type.
正如@HenkHolterman在评论中所指出的,具有复杂类型的价值是拥有一个C#实体,可以将其用作其他包含实体的值(在我的示例中,您可以在供应商实体中拥有一个地址,例如,但它只会被映射为Suppliers表中的一组列。它使得处理复杂类型中的值变得容易。
The disadvantage is precisely that one: you may have to repeat the complex type values many times in the database if it happens that a same Address
(or whatever other type you use) can be shared among different entities.
缺点恰恰在于:如果碰巧在不同实体之间共享相同的地址(或您使用的任何其他类型),则可能必须在数据库中多次重复复杂类型值。
Whether you choose to work with complex types or separate entities is up to you and your design.
无论您选择使用复杂类型还是单独的实体,都取决于您和您的设计。
#2
7
Consider this ContactDetails
class for example:
考虑这个ContactDetails类,例如:
public class ContactDetails
{
public string HomePhone { get; set; }
public string MobilePhone { get; set; }
public string FaxNumber { get; set; }
}
By default, EF will treat ContactDetails
as an Entity. That means that if (for example) you're having a Person
class with a navigation-property of ContactDetails
type, EF will map the Person.ContactDetails
relationship to a different table (because Entity is something that is having an identity of its own, hence other entities may refer to it - and that would require a different table in relational terms).
默认情况下,EF会将ContactDetails视为实体。这意味着如果(例如)你有一个带有ContactDetails类型的导航属性的Person类,EF会将Person.ContactDetails关系映射到另一个表(因为Entity是具有自己身份的东西,因此,其他实体可能会引用它 - 这需要在关系术语中使用不同的表格。
By denoting ContactDetails
as a Complex Type instead, EF will no longer treat it as an entity that requires a relationship and instead map it to the same table of the parent (containing) entity (Person
in my example), effectively making it a Value Object.
通过将ContactDetails表示为复杂类型,EF将不再将其视为需要关系的实体,而是将其映射到父(包含)实体(我的示例中为Person)的同一个表中,从而有效地使其成为值对象。