当使用 Fluent NHibernate 自动映射集合时,如何使子级到父级的外键可为空?
如果我有一个父类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够通过删除第四个祖父母类的流畅映射并允许自动映射器映射该类来解决该问题。之前我不允许自动映射器映射 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 theIAutoMappingOverride
for the Parent, the foreign key in the child class will default to Nullable, rather than Not-Null. Crazy Huh?您是否尝试过:
如果您想将其设为所有外键的默认值,您可以使用以下命令:
Have you tried this:
If you want to make it the default for all your foreign keys, you can use this: