简体   繁体   English

实体框架4的示例:将POCO映射到EAV数据库?

[英]Examples for Entity Framework 4: Mapping POCOs to EAV database?

Our customer advertises products but the product's attributes are vastly different from one another. 我们的客户宣传产品,但产品的属性彼此差异很大。 There are almost as many product "classes" as there are products. 产品“类”几乎和产品一样多。

Originally, we created an EAV database where we added and removed attributes for each "class" of product. 最初,我们创建了一个EAV数据库,我们为每个“类”产品添加和删除了属性。 The whole thing works very well, but the complexity is mind numbing. 整个过程非常有效,但复杂性令人头脑麻木。 This is Database #1. 这是数据库#1。

We finally came up with a set of fields common to represent all products (POCOs), moving the "extra" fields to an XML field "catch all". 我们最终提出了一组共同​​代表所有产品(POCO)的字段,将“额外”字段移动到XML字段“catch all”。 This is Database #2. 这是数据库#2。

Now we have some customers using the old and some customers that will use the new. 现在我们有一些客户使用旧的和一些将使用新的客户。 We really didn't want to update the old until we needed to do so, but we have changes from time to time that simply take longer than necessary because of the EAV structure. 我们真的不想更新旧版本,直到我们需要这样做,但由于EAV结构的原因,我们不时需要更换时间。

Questions: 问题:

  1. Any examples out there on how to code EF to persist POCOs to an EAV databases (where you have a table of field names and a table of data)? 有关如何编写EF以将POCO持久保存到EAV数据库的任何示例(其中您有一个字段名称表和一个数据表)?
  2. Should we just scrap the database and write "normal" tables for all those older customers, given we have changes from time to time? 我们是否应该废弃数据库并为所有老客户编写“正常”表格,因为我们不时会有变化?

Normal of course is referring to the Boyce Codd Normal Form. 正常当然是指Boyce Codd Normal Form。
We handled the older database by bleeding its structure into the software, then mapping to POCOs in the repository. 我们通过将其结构放入软件,然后映射到存储库中的POCO来处理旧数据库。

EAV tables are generally a messy business, and Joe Celko (in Avoiding the EAV of Destruction ) and a lot of other industry experts (eg Five Simple Database Design Errors You Should Avoid ) rightfully warn against using EAV constructs too much. EAV表通常是一个混乱的业务,Joe Celko( 避免毁灭的EAV )和许多其他行业专家(例如, 你应该避免的五个简单数据库设计错误 )正确地警告不要使用EAV结构太多。

All database critics aside: how would an object in .NET based on such an EAV even look like?? 除了所有数据库批评者:基于这样的EAV的.NET中的对象怎么样才会看起来像? It could have any number of properties of any type, so basically, it would have to be a "universal" object that can take on any shape, pretty much. 它可以有任意类型的任何数量的属性,所以基本上,它必须是一个“通用”对象,可以采取任何形状,几乎。

This thought makes my skin crawl and goes against the most basic concepts of a strongly typed language - yes, you can do stuff like that in a dynamic language like Ruby and Python, but in C#? 这种想法使我的皮肤爬行并违背了强类型语言的最基本概念 - 是的,你可以在像Ruby和Python这样的动态语言中做这样的事情,但是在C#中?

The only viable option you have might come with the new "dynamic" type in .NET 4.0 and the ExpandoObject - an object that can take on any shape, have any properties of any type, and basically be whatever you want it to be. 唯一可行的选项可能是.NET 4.0中的新“动态”类型和ExpandoObject - 一个可以呈现任何形状的对象,具有任何类型的任何属性,并且基本上是您想要的任何属性。

You could imagine a mapping between an EAV structure in SQL Server and an ExpandoObject in C# 4.0 - but I highly doubt the EF team has done anything in this respect, and quite honestly, I don't think they will any time soon. 您可以想象SQL Server中的EAV结构与C#4.0中的ExpandoObject之间的映射 - 但我非常怀疑EF团队在这方面做了什么,而且老实说,我认为他们不会很快。 But this might be a possibility for you to explore. 但这可能是你探索的可能性。

For the ExpandoObject, see: 对于ExpandoObject,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM