问题描述
类雇员
虚拟IList< ChecklistItem> HasInitialed {get;私人设置}
Class ChecklistItem
virtual Employee InitialedBy {get;组; }
生成这个表时,我得到这些表
Employee
Id
ChecklistItem
Id
InitialedBy_id< ----
Employee_id< ----
ChecklistItem对于Employee有两个外键,我假定Employee_id来映射ChecklistItem .Employee和InitialedBy_id来映射ChecklistItem.InitialedBy。我怎样才能告诉NHibernate,这是相同的双向关系,只需要1个外键?
我对NHibernate是一种新的一般,但这似乎是应该是相当标准的。
当我配置数据库来生成正确的模式时,这是我想到的,是吗?
$ b
.Override< Employee>(map => map
.HasMany< ChecklistItem>(x => x.HasInitialed)
.KeyColumn(InitialedBy_id))
如果这是正确的,有没有办法选择基于ChecklistItem属性名称(a lambda)的KeyColumn?
作为所有hasmanies的约定
class HasManyConvention:IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key .Column(((ICollectionInspector)instance).Name +_id);
$ b $ p
$ b class HasManyConvention:IHasManyConvention,IHasManyConventionAcceptance
{
public void Accept(IAcceptanceCriteria< IOneToManyCollectionInspector> criteria)
{
criteria.Expect(x => x.Name ==HasInitialed); //(和/或实体类型
)
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.Column(((ICollectionInspector)instance) +_id);
$ b $ p $
$ b class ReferenceConvention:IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Column (instance.Name +_id);
}
}
I have this setup (condensed for brevity)
Class Employee
virtual IList<ChecklistItem> HasInitialed { get; private set; }
Class ChecklistItem
virtual Employee InitialedBy { get; set; }
When this is generated I get these tables
Employee
Id
ChecklistItem
Id
InitialedBy_id <----
Employee_id <----
The ChecklistItem has 2 foreign keys for Employee, I'm assuming Employee_id to map ChecklistItem.Employee and InitialedBy_id to map ChecklistItem.InitialedBy. How can I tell NHibernate that this is the same bidirectional relationship and only requires 1 foreign key?
I'm kind of new to NHibernate in general, but this seems like it should be pretty standard.
This is what I've come up with when I'm configuring my database to generate the right schema, is it right?
.Override<Employee>(map => map
.HasMany<ChecklistItem>(x => x.HasInitialed)
.KeyColumn("InitialedBy_id"))
If that is right, is there a way to choose KeyColumn based on the ChecklistItem's property name (a lambda)?
解决方案 as a convention for all hasmanies
class HasManyConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
}
}
or only for this
class HasManyConvention : IHasManyConvention, IHasManyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IOneToManyCollectionInspector> criteria)
{
criteria.Expect(x => x.Name == "HasInitialed"); // and/or entity type
}
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
}
}
convention for Manytoone/References
class ReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Column(instance.Name + "_id");
}
}
这篇关于流利的NHibernate Automappings为1关系生成2个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!