使用 Fluent-nhibernate,是否有约定使一对多关系中的外键列不为空?
我正在使用 Fluent-nhibernate 约定来映射我的实体:
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
instance.Cascade.AllDeleteOrphan();
}
}
因此该约定生成以下休眠映射:
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="ParentType, ParentTypeAssembly" table="ParentTable">
<id name="ParentIDID" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="ParentID" />
<generator class="identity" />
</id>
<bag cascade="all-delete-orphan" name="Childs" mutable="true">
<key>
<column name="ParentID" />
</key>
<one-to-many class="ChildType, ChildTypeAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
</hibernate-mapping>
如何修改约定以使子表中的 ParentID 不为空?
所以它看起来像这样:
<key>
<column name="ParentID" not-null="true" />
</key>
I'm using fluent-nhibernate conventions to map my entities:
public class HasManyConvention : IHasManyConvention
{
public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "ID");
instance.Cascade.AllDeleteOrphan();
}
}
so this convention generate the following hibernate mapping :
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="ParentType, ParentTypeAssembly" table="ParentTable">
<id name="ParentIDID" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="ParentID" />
<generator class="identity" />
</id>
<bag cascade="all-delete-orphan" name="Childs" mutable="true">
<key>
<column name="ParentID" />
</key>
<one-to-many class="ChildType, ChildTypeAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
</hibernate-mapping>
How can i modify the convention to make the ParentID in the child table not null?
So it will look like this :
<key>
<column name="ParentID" not-null="true" />
</key>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
公约中似乎没有任何方法可以做到这一点。查看测试 为了实现 HasManyConvention 和 IOneToManyCollectionInstance 接口上的方法,我没有看到任何设置 not-null 的方法。
不过,您可能想看看这个问题。
There does not appear to be any way to do this in the convention. Looking at the tests for implementing the HasManyConvention and the methods on the IOneToManyCollectionInstance interface I don't see any way to set not-null.
You might want to take a look at this question though.