C# nHibernate,不存在具有给定标识符的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/695931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:50:19  来源:igfitidea点击:

nHibernate, No row with the given identifier exists

c#nhibernatenhibernate-mapping

提问by Craig

I have a mapping along the lines of this.

我有一个沿着这条线的映射。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model.Entities" schema="etl" assembly="Model" default-lazy="false">
  <class name="Model.Entities.DataField, Model" table="mdm_field">
    <id name="FieldId" column="field_id" type="int">
      <generator class="native" />
    </id>
    <many-to-one name="KeyField" class="Model.Entities.Key, Model" column="field_id" />
  </class>
</hibernate-mapping>

Now in the database the field_id in the mdm_field table sometimes has a value that does not exist in the related key_field table, so it is basically broken referential integrity. Because of this when I load the entity I get an error "No row with the given identifier exists". How do I configure the mapping to work with this situation so it will not die on this situation.

现在在数据库中mdm_field表中的field_id有时会出现相关key_field表中不存在的值,所以基本破坏了参照完整性。因此,当我加载实体时,出现错误“不存在具有给定标识符的行”。我如何配置映射以适应这种情况,以便它不会在这种情况下死亡。

采纳答案by Craig

Ok, I found the answer. Add the

好的,我找到了答案。添加

not-found="ignore"

attribute to the property KeyField:

属性的属性KeyField

<many-to-one name="KeyField" not-found="ignore" class="Model.Entities.Key, Model" column="field_id" />

回答by Shagglez

In my case the problem was because a foreign key constraint was not enforced by MyISAM engine and therefore one of the rows ended up pointing to a non-existant value and the proxy threw an exception. I would recommend checking your dataset is consistent in this case.

在我的情况下,问题是因为 MyISAM 引擎没有强制执行外键约束,因此其中一行最终指向一个不存在的值并且代理抛出异常。在这种情况下,我建议检查您的数据集是否一致。

回答by Rodolpho Brock

Try that...

试试那个...

public void Override(ModelMapper modelMapper) {
    modelMapper.Class<T>(c => { 
        c.ManyToOne(m => m.FKObj, r => {
            r.Column("FKColumn");
            r.NotFound(NotFoundMode.Ignore); // THIS IS IMPORTANT!!!
        });
    });
}