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

发布于 2024-09-07 03:25:42 字数 570 浏览 6 评论 0原文

如果我有一个父类:

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

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

和一个像这样的子类:

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

每当我让 Fluent NHibernate 的自动映射器命中这些家伙时,它都会使 Child 类具有不可为 null 的外键。我更改了一些自动映射约定和某些类的一些覆盖,但对于这个特定的对,只有 Parent 类具有覆盖。该覆盖未指定如何映射 Parent 类的集合部分。

使集合的子集合中的外键不可为空是默认行为,还是我搞砸了?

如何在映射覆盖类中指定子外键可为空?

和平!

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. 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. The override does not specify how to map the collection part for the Parent class.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人接听 2024-09-14 03:25:44

我能够通过删除第四个祖父母类的流畅映射并允许自动映射器映射该类来解决该问题。之前我不允许自动映射器映射 GrandParent 类,因为我对该类有流畅的映射。不知何故,流畅映射和自动映射之间的冲突导致 Child 中的外键不可为空。

我不能说我完全理解出了什么问题,因此建议在遵循我的解决方案时务必小心。但我可以说,结合 Fluent Mappings 和 Automappings 肯定会增加调试持久性问题的复杂性。

替代解决方案:我又遇到了这个问题两次,我注意到如果我在父项的 IAutoMappingOverride 中调用 KeyColumn(string columnName) ,则外键子类将默认为 Nullable,而不是 Not-Null。疯了啊?

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. Before I had not allowed the automapper to map the GrandParent class because I had a fluent mapping of that class. Somehow this conflict between fluent and auto mappings caused the foreign key in Child to be not-nullable.

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.

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. Crazy Huh?

只是在用心讲痛 2024-09-14 03:25:43

您是否尝试过:

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

如果您想将其设为所有外键的默认值,您可以使用以下命令:

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

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();
       }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文