什么是实体框架中的复杂类型,何时使用它? [英] What is a complex type in entity framework and when to use it?

查看:107
本文介绍了什么是实体框架中的复杂类型,何时使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试阅读msdn 文章复杂类型。但它并不能解释何时使用它。还有一个关于复杂类型和何时使用它们的网络的综合解释。

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文章中...所以你基本上想要一个简单的说明:

The lengthy explanation is in the MSDN article you linked... so you basically want an easy explanation:

复杂类型是一组存在于其自己的C#对象中的属性,但是映射到已存在的表(包含该表的实体)的列,而不是有自己的表(需要一个键等)。

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.).

所以想象你想要这个数据库表:

So imagine you want this table on the database:

Orders
----------
Id (bigint)
Name (varchar)
Street (varchar)
Region (varchar)
Country (varchar)

但是在C#实体中想要这个结构:

But want this structure in the C# entities:

class Order
{
   long Id;
   string Name;

   struct Address
   {
     string Street;
     string Region;
     string Country;
   }
}

所以这里 code>将是一个复杂的类型:它不会自己存在(在数据库上不会有 Addresses 表)...它只会存在作为订单表之间的一组列。

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.

正如@HenkHolterman在评论中所指出的那样,具有复杂类型的是具有单个C#实体,可以用作其他包含实体的值(在我的示例中,您可以在地址 >供应商实体,例如,它将被映射为 Suppliers 表中的一组列。它使得使用复杂类型的值很容易。

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.

缺点是正是这样一个:您可能必须在数据库中重复复杂的类型值多次如果发生相同的地址(或您使用的任何其他类型)可以在不同实体之间共享。

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.

这篇关于什么是实体框架中的复杂类型,何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆