一比一的映射问题与NHibernate /流利的:外键不updateing [英] One-to-one Mapping issue with NHibernate/Fluent: Foreign Key not updateing

查看:88
本文介绍了一比一的映射问题与NHibernate /流利的:外键不updateing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:父母与子女类。一到父母与子女之间的一个关系。家长有一个FK属性,它引用了儿童的主键。 code如下:

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. 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:

父映射

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

儿童映射:

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

如果我做这样的事情(伪code)...

If I do something like (pseudo code) ...

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

...我得到一个错误:无效的索引3本SqlParameterCollection以计数= 3

... I get an error: "Invalid index 3 for this SqlParameterCollection with Count=3"

如果我更改父'引用'线如下(即提供孩子财产,我指着名称):

If I change the parent 'References' line as follows (i.e. provide the name of the child property I'm pointing at):

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

我得到的错误:无法解析属性:NHTestChildId 于是,我尝试了HasOne()'基准设置,如下所示:

I get the error: "Unable to resolve property: NHTestChildId" So, I tried the 'HasOne()' reference setting, as follows:

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

在此布置中,保存作品(和数据在分贝被通缉),但加载未能找到子实体。检查SQL NHibernate的产生说明我的NHibernate是假设母公司的主键的链接子(即负载连接条件是parent.NHTestParentId = child.NHTestChildId)出现我指定的ForeignKey的被忽视 - 事实上,如果我可以设置的任何值(甚至非实存场),并没有出现错误 - 联接只是总是失败,并没有儿童返回

In this arrangement the save works (and data in db is as wanted), but loading fails to find the child entity. Inspecting the SQL Nhibernate produces shows me that NHibernate is assuming the Primary key of the parent is the link to the child (i.e. 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.

我已经尝试了一些上述细微变化。现在看来似乎应该是一件简单的事情来实现。任何想法?

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);

又见 <一href="http://stackoverflow.com/questions/2298026/indexoutofrangeexception-deep-in-the-bowels-of-nhibernate/2311256#2311256">http://stackoverflow.com/questions/2298026/indexoutofrangeexception-deep-in-the-bowels-of-nhibernate/2311256#2311256

这篇关于一比一的映射问题与NHibernate /流利的:外键不updateing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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