简体   繁体   English

当使用Fluent NHibernate自动映射集合时,如何使子代的父代外键为空?

[英]When auto-mapping a collection with Fluent NHibernate, how do you make the child's foreign key to the parent nullable?

If I have a parent class: 如果我有家长班:

public class Parent
{
    public Parent()
    {
        Children = new List<Child>();
    }

    IList<Child> Children {get; private set;}
}

and a child class like so: 和一个像这样的孩子班:

public class Child
{
     public SomeThirdClass Friend {get; set;}
}

Whenever I let the Fluent NHibernate's automapper hit these guys, it makes the Child class have a non-nullable foreign key. 每当我让Fluent NHibernate的自动映射器击中这些家伙时,这都会使Child类具有不可为空的外键。 I have altered a few automapping conventions and some overrides for certain classes, but for this particular pair only the Parent class has an override. 我为某些类更改了一些自动映射约定和一些替代,但是对于此特定对,只有Parent类具有替代。 The override does not specify how to map the collection part for the Parent class. 重写未指定如何映射Parent类的集合部分。

Is making a foreign key not-nullable in the child of a collection the default behavior, or have I F'ed something up? 使外键在集合的子级中不可为空是默认行为,还是我搞砸了?

How would one specify in a mapping override class that the child foreign key is nullable? 如何在映射覆盖类中指定子外键可为空?

PEACE! 和平!

Have you tried this: 您是否尝试过:

   public class ChildMap : IAutoMappingOverride<Child>
   {
       public void Override(AutoMapping<Child> mapping)
       {
           mapping.References(x => x.OtherThing)
               .Nullable();
       }
   }

If you want to make it the default for all your foreign keys, you can use this: 如果要将其设置为所有外键的默认值,则可以使用以下命令:

   public class NullableFKConvention : IReferenceConvention
   {
       public void Apply(IManyToOneInstance instance)
       {
           instance.Nullable();
       }
   }

I was able to solve the problem by removing a fluent mapping of a 4th GrandParent class, and allowing the automapper to map that class. 通过删除第4个GrandParent类的流利的映射并允许自动映射器映射该类,我能够解决该问题。 Before I had not allowed the automapper to map the GrandParent class because I had a fluent mapping of that class. 在我不允许自动映射器映射GrandParent类之前,因为我对该类进行了流利的映射。 Somehow this conflict between fluent and auto mappings caused the foreign key in Child to be not-nullable. 流利的映射和自动映射之间的冲突以某种方式导致Child中的外键不可为空。

I can't say that I completely understand what went wrong, so caution is advised when following my solution. 我不能说我完全理解出了什么问题,因此在遵循我的解决方案时请多加注意。 But I can say that combining Fluent Mappings and Automappings definitely increases the complexity of debugging persistence issues. 但是我可以说,将Fluent映射和自动映射结合起来肯定会增加调试持久性问题的复杂性。

Alternate Solution: I came across this problem again another two times, and I noticed that if I call KeyColumn(string columnName) in the IAutoMappingOverride for the Parent, the foreign key in the child class will default to Nullable, rather than Not-Null. 替代解决方案:我又两次遇到此问题,并且注意到如果我在IAutoMappingOverride为Parent调用KeyColumn(string columnName) ,则子类中的外键将默认为Nullable,而不是Not-Null。 Crazy Huh? 疯了吗?

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

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