秒自拍至自身关系的实体框架 [英] Second Self-To-Self relationship in Entity Framework

查看:150
本文介绍了秒自拍至自身关系的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个领域类

public class Incident
{
    [Key]
    public virtual int IncidentId { get; set; }

    [Display(Name = "Parent Incident")]
    public virtual Incident ParentIncident { get; set; }

    [Display(Name = "Related Claim")]
    public virtual Incident ClaimIncident { get; set; }

 }

其它性能为了简化,省略

Other properties are omitted for simplicity.

当我在地方刚 ParentIncident ,一切正常。现在我已经加入 ClaimIncident 的类。我试图更新使用实体框架4.3($ P $租赁前)迁移选项我的数据库,但我得到一个错误,即EF不知道如何映射事件到事件。

When I had just ParentIncident in place, everything worked fine. Now I have added ClaimIncident to the class. I am attempting to update my database using the Entity Framework 4.3 (PreRelease) Migrations option, but I am getting an error, that EF doesn't know how to map Incident to Incident.

为什么要引用同一个类实例一次只为每个类允许的,而当我介绍了第二个,它突然没有线索如何参考呢?我怎样才能纠正模型类?

Why referencing the same class instance once is allowed per class, and when I introduce the second one, it suddenly has no clue of how to reference it? And how can I correct the model class?

推荐答案

这是什么,我认为正在发生的事情。当你只有ParentIncident code首先由约定映射这是一个自我引用一个一对多的单向独立相关的一端。许多专业术语在那里 - 让我解释一下:

This is what I think is happening. When you only had ParentIncident Code First was mapping this by convention as one end of a self-referencing one-to-many unidirectional independent association. Lots of jargon there--let me explain:

  • 在自引用的:因为是从问题apprarant,该Indicent实体是在关系的两端
  • 一对多的:每个Indicent具有单个ParentIncident和一个事件可以是父许多事件
  • 单向:有一个从事件导航属性其父(ParentIncident),但孩子的事件没有反向的集合导航性能
  • Indpendent协会:由于是在类的外键属性,EF在数据库中创建(称为ParentIncident_IncidentId)的FK列,但它没有映射到任何物体属性

现在,当您添加ClaimIncident财产这改变了方式code首先尝试映射。现在创建一个自引用一到一个双向FK关联:

Now, when you add the ClaimIncident property this changes the way Code First tries to map. It now creates a self-referencing one-to-one bidirectional FK association:

  • 1对,因为它认为从事件两个导航属性​​,事件和假设,这些都是一样的关系两个方面。因为无论是集合属性,关系是一对之一。
  • 双向再次因为现在关系的每端nvaigation属性。
  • FK,因为一到一对一的关系变得PK来PK和EF始终将此视为自FK(这是一个PK)的FK关系映射。

不过,$ C C首先$想不通其中这个一比一的关系方面应该是主要目的,而这应该是相关的结束。这确实很重要,有时...所以$ C C首先$抛出。

But Code First can't figure out which side of this one-to-one relationship should be the principal end, and which should be the dependant end. And this does matter sometimes...so Code First throws.

好了,所以我打算从你真的不想在这里一比一的关系线程模型来推断。 (一到一自引用PK来PK的关系是一种毫无意义的,所以你可能会说,$ C C $首先不应该创建它,但$ C C首先$不是那么聪明。)

Okay, so I'm going to infer from the thread and your model that you don't really want a one-to-one relationship here. (A one-to-one self-referencing PK to PK relationship is kind of pointless, so you could argue that Code First shouldn't create it, but Code First isn't that smart.)

所以,你把FKS研究。两件事情发生。首先,$ C C首先$现在假定这些必须是一对多的关系了。第二,$ C C首先$现在拥有的FK属性的名称,又使用该名称在数据库中的FK列名。但是,这个名字是不一样的,作为相关联的独立已经创建了FK列 - 这不是ParentIncident_IncidentId。这意味着,迁移将不得不放弃此列,并创建一个新的...导致数据丢失。

So you put the FKs in. Two things happen. First, Code First now assumes that these must be one-to-many relationships again. Second, Code First now has a name for the FK property and in turn uses that name to name the FK column in the database. But that name is not the same as the FK column already created for the independent associated--it's not ParentIncident_IncidentId. This means that Migrations would have to drop this column and create a new one...resulting in data loss.

要告诉C首先$ C $,你真的只是想两个自参考,单向,一对许多人来说,独立的协会,使用流利的映射:

To tell Code First that you really just want two self-referencing, unidirectional, one-to-many, independent associations, use this fluent mapping:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Incident>()
        .HasOptional(e => e.ParentIncident)
        .WithMany();

    modelBuilder.Entity<Incident>()
        .HasOptional(e => e.ClaimIncident)
        .WithMany();
}

现在迁移应该正确地更新数据库。现在,它必须说,你可能要考虑改用FK关联,你要么在这种情况下需要做的,迁移一些数据的运动,只接受数据损失,如果有任何,或者告诉code首先保持使用它previously生成的FK列名。

Now Migrations should correctly update your database. Now, it must be said that you may want to consider changing to FK associations, in which case you either need to do some data motion in Migrations, just accept the data loss if there is any, or tell Code First to keep using the FK column name that it previously generated.

这篇关于秒自拍至自身关系的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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