简体   繁体   English

NHibernate / Fluent的一对一映射问题:外键未更新

[英]One-to-one Mapping issue with NHibernate/Fluent: Foreign Key not updateing

Summary: Parent and Child class. 简介:父母和孩子班。 One to one relationship between the Parent and Child. 父母与子女之间一对一的关系。 Parent has a FK property which references the primary key of the Child. 父级具有FK属性,该属性引用子级的主键。 Code as follows: 代码如下:

  public class NHTestParent
  {
    public virtual Guid NHTestParentId { get; set; }
    public virtual Guid ChildId
    {
      get
      {
        return ChildRef.NHTestChildId;
      }
      set { }
    }
    public virtual string ParentName { get; set; }

    protected NHTestChild _childRef;
    public virtual NHTestChild ChildRef
    {
      get
      {
        if (_childRef == null)
          _childRef = new NHTestChild();
        return _childRef;
      }
      set
      {
        _childRef = value;
      }
    }
  }    

  public class NHTestChild
  {
    public virtual Guid NHTestChildId { get; set; }
    public virtual string ChildName { get; set; }
  }

With the following Fluent mappings: 使用以下Fluent映射:

Parent Mapping 父映射

  Id(x => x.NHTestParentId);
  Map(x => x.ParentName);
  Map(x => x.ChildId);
  References(x => x.ChildRef, "ChildId").Cascade.All();

Child Mapping: 儿童制图:

  Id(x => x.NHTestChildId);
  Map(x => x.ChildName);

If I do something like (pseudo code) ... 如果我做类似(伪代码)的操作...

HTestParent parent = new NHTestParent();
parent.ParentName = "Parent 1";
parent.ChildRef.ChildName = "Child 1";
nhibernateSession.SaveOrUpdate(aParent);
Commit;

... I get an error: "Invalid index 3 for this SqlParameterCollection with Count=3" ...我收到一个错误:“此SqlParameterCollection的索引3无效,计数为3”

If I change the parent 'References' line as follows (ie provide the name of the child property I'm pointing at): 如果我按如下方式更改父级“参考”行(即提供我指向的子级属性的名称):

References(x => x.ChildRef, "ChildId").PropertyRef("NHTestChildId").Cascade.All();

I get the error: "Unable to resolve property: NHTestChildId" So, I tried the 'HasOne()' reference setting, as follows: 我收到错误消息:“无法解析属性:NHTestChildId”因此,我尝试了“ HasOne()”引用设置,如下所示:

HasOne<NHTestChild>(x => x.ChildRef).ForeignKey("ChildId").Cascade.All().Fetch.Join();

In this arrangement the save works (and data in db is as wanted), but loading fails to find the child entity. 在这种安排下,保存工作正常(并且db中的数据是所需的),但是加载无法找到子实体。 Inspecting the SQL Nhibernate produces shows me that NHibernate is assuming the Primary key of the parent is the link to the child (ie load join condition is "parent.NHTestParentId = child.NHTestChildId). The 'ForeignKey' I specified appears to be ignored - if fact I can set any value (even a non-existance field) and no error occurs - the join just always fails and no child is returned. 检查SQL Nhibernate产生的SQL显示,NHibernate假设父级的主键是到子级的链接(即,加载连接条件为“ parent.NHTestParentId = child.NHTestChildId”)。我指定的'ForeignKey'似乎被忽略了-如果事实上我可以设置任何值(甚至是不存在的字段)并且没有错误发生-联接总是失败并且没有子级返回。

I've tried a number of slight variations on the above. 我在上面尝试了一些细微的变化。 It seems like it should be a simple thing to achieve. 看起来应该很简单。 Any ideas? 有任何想法吗?

you are mapping the same column twice, and that is not allowed. 您两次映射同一列,这是不允许的。 Remove the following from the parent class 从父类中删除以下内容

Map(x => x.ChildId);

see also IndexOutOfRangeException Deep in the bowels of NHibernate 参见IndexOutOfRangeException在NHibernate的深处

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

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