简体   繁体   English

流利的NHibernate - 一到一个(1 - 0..1)关系

[英]Fluent NHibernate - one to at most one (1 – 0..1) Relationship

Im just trying to work out if its possible to map a 1 - 0..1 (one to at most one \\ one to zero or one) relationship using fluent NHibernate without the use of a Private Collection. 我只是试图找出它是否可以使用流畅的NHibernate映射1 - 0..1(一个到最多一个\\一个到零或一个)关系而不使用私人收藏。 An example of the class structure is below: 类结构的一个例子如下:

    public class ClassA
    {
        public int ClassAId { get; set; }

        public string SomeDetails { get; set; }

        public ClassB ClassB { get; set; }

    }

    public class ClassB
    {
        public ClassA ClassA { get; set; }

        public int ClassBId { get; set; }

        public string SomeChildDetails { get; set; }
    }

In this example, ClassA can have a ClassB, or ClassB can be null. 在此示例中,ClassA可以具有ClassB,或者ClassB可以为null。

The DB structure would look something like: DB结构看起来像:

   ClassA Table
   -------------
   int ClassA_ID  PK
   string SomeDetails

   ClassB Table
   ------------
   int ClassA_Id   PK  FK
   string SomeChildDetails

In this situation, you cannot use HasOne(x => x.ClassB).Cascade.All(); 在这种情况下,您不能使用HasOne(x => x.ClassB).Cascade.All(); as this assumes it must always have one. 因为这假定它必须始终有一个。

Is there a way to do this without having a one to many relationship with a private IList on ClassA and having the getter of the ClassB property getting the first entry in the list. 有没有办法做到这一点,没有与ClassA上的私有IList的一对多关系,并让ClassB属性的getter获取列表中的第一个条目。

Ta

R [R

Apart from marking all your properties on mappings as virtual , References should do the trick: 除了将映射上的所有属性标记为virtualReferences应该可以解决这个问题:

References(x => x.ClassB).Nullable().Cascade.All();

The Fluent NHibernate's documentation says it's the equivalent of many-to-one relationship, but works with single properties too, which ends up being 1-0..1 relationship. Fluent NHibernate的文档说它相当于多对一关系,但也适用于单个属性,最终成为1-0..1关系。 You could try the HasOne with Nullable() modifier, but it says in the docs that you generally should use References instead. 您可以尝试使用Nullable()修饰符的HasOne ,但它在文档中说您通常应该使用References

[EDIT to the comment]: [编辑评论]:

As far as I remember, the default foreign key naming policy for NHibernate is keyname_id , which you can either change by implementing you own ForeignKeyConvention (this one is for all mappings). 据我keyname_id ,NHibernate的默认外键命名策略是keyname_id ,你可以通过实现自己的ForeignKeyConvention来改变它(这个是针对所有映射的)。 Let's say your foreign key policy is TableNameID, which would be 'ClassBID': 假设您的外键策略是TableNameID,它将是'ClassBID':

internal class MyFkeyConvention : ForeignKeyConvention
{
    protected override string GetKeyName(FluentNHibernate.Member property, Type type)
    {
        if(property != null)
            return property.Name + "ID";
        return type.Name + "ID";
    }
}

or just use this one if you need it only in one place: 或者只在一个地方需要它时使用这个:

References(x => x.ClassB).Column("YourForeignKeyColumnName").Nullable().Cascade.All();

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

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