造型多态关联数据库第一VS code-第一 [英] Modelling polymorphic associations database-first vs code-first

查看:151
本文介绍了造型多态关联数据库第一VS code-第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在其中一个表中包含的记录,可以是孩子其他几个表的数据库。它有一个软的外键由所有者的ID和表名。这个(反)模式是知道的多态关联。我们知道这是不是最好的数据库设计永我们将改变它在适当的时候,而不是在不久的将来。让我给一个简单的例子:

We have a database in which one table contains records that can be child to several other tables. It has a "soft" foreign key consisting of the owner's Id and a table name. This (anti) pattern is know as "polymorphic associations". We know it's not the best database design ever and we will change it in due time, but not in the near future. Let me show a simplified example:

两者事件产品有在注释记录中。正如你看到的,有没有硬约束的FK

Both Event, Person, and Product have records in Comment. As you see, there are no hard FK constraints.

在实体框架可以通过sublassing 到 EventComment 注释等,以支持这种模式,让事件有一个 EventComments 收集等。

In Entity Framework it is possible to support this model by sublassing Comment into EventComment etc. and let Event have an EventComments collection, etc.:

子类和关联从数据库生成的基本模型后手动添加。 所有者code 在此 TPH 模型鉴别。请注意,事件产品完全不同的实体。它没有意义有一个共同的基类他们。

The subclasses and the associations are added manually after generating the basic model from the database. OwnerCode is the discriminator in this TPH model. Please note that Event, Person, and Product are completely different entities. It does not make sense to have a common base class for them.

这是数据库第一。我们的现实生活中的模型是这样工作的,没有问题。

This is database-first. Our real-life model works like this, no problem.

确定。现在,我们要移动到code-第一。于是我开始了逆向工程数据库到code第一个模型(EF电动工具),并继续创建子类和测绘协会和继承。试图连接到Linqpad模型。这就是麻烦开始的时候。

OK. Now we want to move to code-first. So I started out reverse-engineering the database into a code first model (EF Power Tools) and went on creating the subclasses and mapping the associations and inheritance. Tried to connect to the model in Linqpad. That's when the trouble started.

当试图用这种模式它抛出一个 InvalidOperationExeception

When trying to execute a query with this model it throws an InvalidOperationExeception

国外关键部件OWNERID不在类型EventComment声明的属性。验证它尚未明确从模型中排除,并且它是一个有效的基本属性。

The foreign key component 'OwnerId' is not a declared property on type 'EventComment'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

这发生在我的双向关联和 OWNERID 被映射为一个属性注释。在我的 EventMap 类的映射( EntityTypeConfiguration<事件> )看起来是这样的:

This happens when I have bidirectional associations and OwnerId is mapped as a property in Comment. The mapping in my EventMap class (EntityTypeConfiguration<Event>) looks like this:

this.HasMany(x => x.Comments).WithRequired(c => c.Event)
    .HasForeignKey(c => c.OwnerId);

于是,我就在映射模型,而不 OWNERID 的关联关系:

this.HasMany(x => x.Comments).WithRequired().Map(m => m.MapKey("OwnerId"));

这将引发 MetaDataException

指定的模式是无效的。错误:
  (10,6):错误0019:在类型每个属性名称必须是唯一的。属性名称OWNERID'已经定义。
  (11,6):错误0019:在类型每个属性名称必须是唯一的。属性名称OWNERID'已经定义。

Schema specified is not valid. Errors: (10,6) : error 0019: Each property name in a type must be unique. Property name 'OwnerId' was already defined. (11,6) : error 0019: Each property name in a type must be unique. Property name 'OwnerId' was already defined.

如果我删除两个三个实体评论协会这是确定的,但当然这不是治愈。

If I remove two of the three entity-comment associations it is OK, but of course that's not a cure.

一些进一步的细节:


  • 可以通过添加的DbContext发电机项目创建从EDMX工作的DbContext模型(code秒)。 (这将是一个变通暂时)。

  • 当我出口工作code-第一个模型(一个协会)到EDMX( EdmxWriter )的关联似乎是在存储模型,而在原来EDMX他们是概念模型的一部分​​。

  • It is possible to create a working DbContext model ("code second") from the edmx by adding a DbContext generator item. (this would be a work-around for the time being).
  • When I export the working code-first model (with one association) to edmx (EdmxWriter) the association appears to be in the storage model, whereas in the original edmx they are part of the conceptual model.

所以,我怎样才能建立这种模式code-第一?我认为关键是如何指导code-首先向协会在概念模型映射,而不是存储模型。

So, how can I create this model code-first? I think the key is how to instruct code-first to map the associations in the conceptual model, not the storage model.

推荐答案

我个人坚持使用数据库先上就是这种复杂的任何模式使用EF的时候。我在问候code首先与复杂的架构问题。也许新版本是好一点,但令人担忧的是如何尝试和code复杂的关系似乎不太向前伸直,然后让发动机产生它。此外,当关系变得这种复杂的我倾向于避免试图与EF产生,并尝试使用存储过程可能出现性能瓶颈的更方便地解决。

I personally stick with Database first when using EF on any schema that is this level of complexity. I have had issues with complex schemas in regards to code first. Maybe the newer versions are a little better, but worrying how to try and code complex relationships seems less straight forward then allowing the engine to generate it for you. Also when a relationship gets this complex I tend to avoid trying to generate it with EF and try and use stored procedures for easier troubleshooting of performance bottlenecks that can arise.

这篇关于造型多态关联数据库第一VS code-第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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