代码之家  ›  专栏  ›  技术社区  ›  Bruno Reis

流畅的NHibernate&一对一

  •  25
  • Bruno Reis  · 技术社区  · 16 年前

    很长一段时间以来,我一直在寻找一个关于如何正确实现 使用Fluent NHibernate进行映射。

    我找到的大多数资源都说:

    我想你的意思是多对一

    然而,实际上没有人给出如何正确实现一对一关系的示例。

    所以 你能用Fluent NHibernate给出一个一对一的映射示例吗 ?

    注意:我对人们说“你的模型是什么,你可能真的需要什么”不感兴趣 一对一

    更确切地说,, 我知道语法 . 这是我能找到的唯一一件我自己搜索的东西。我要找的是一个更完整的例子,包括((非常)简单的)数据库设置,以及参与关系的所有实体的整个映射,我认为这些实体的大小对于堆栈溢出是合理的。

    4 回复  |  直到 10 年前
        1
  •  19
  •   Bruno Reis    11 年前

    我已经解决了我的问题。

    我还写了一篇关于这个问题的详细文章,你可以在以下网站上找到: http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/index.html

    您将发现一个场景,其中我们需要一对一的关系、我们想要的数据库模式、满足NHibernate需求所需的模型代码,以及 流畅的 对应于情况的映射。

        2
  •  3
  •   Mohit Arora    11 年前
    these are the two classes.
    
    public class A
    {
      public virtual int Id {get;set;}
      public virtual string P1 {get;set;}
      public virtual string P2 {get;set;}
      public virtual string P3 {get;set;}
      public virtual B child { get; set; }
    }
    
    public class B
    {
      public virtual int Id {get;set;}
      public virtual string P4 {get;set;}
      public virtual string P5 {get;set;}
      public virtual string P6 {get;set;}
      public virtual A parent;
    }     
    

    public AMap()
        {
          /* mapping for id and properties here */
          HasOne(x => x.child)
              .Cascade.All();
        }
    
        public BMap()
        {
          /* mapping for id and properties here */
          References(x => x.parent)
              .Unique();
        }
    
        3
  •  2
  •   Jamie Ide    16 年前

    This is the best example 我见过。希望它能满足你的需要。

        4
  •  -1
  •   James Gregory    16 年前

    HasOne(x => x.Prop)

    推荐文章