如何使用 IHasManyConvention Fluent NHibernate 约定在 HasMany 映射上设置 PropertyRef?
目前,我正在使用 Fluent NHibernate 生成数据库架构,但我希望 HasMany 关系中的实体指向不同的列以供参考。 IE,这就是 NHibernate 将在创建 DDL 中生成的内容:
alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);
这就是我想要的:
alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);
其中 Stable.ID 将是主键,而 Stable.EntityId 只是我设置的另一列。
我已经有一个如下所示的类:
public class ForeignKeyReferenceConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Cascade.All();
//What goes here so that I can change the reference column?
}
}
我需要做什么才能更改参考列?
作为一个例子,下面是 IReferenceConvention 的代码看起来像做同样的事情:
public class MyReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.PropertyRef("EntityId");
instance.Cascade.All();
}
}
编辑: instance.Key.Column("EntityId")
不是解决方案。
Currently I'm using Fluent NHibernate to generate my database schema, but I want the entities in a HasMany relationship to point to a different column for the reference. IE, this is what NHibernate will generate in the creation DDL:
alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (Id);
This is what I want to have:
alter table `Pony` add index (Stable_ID),
add constraint Ponies_Stable foreign key (Stable_Id)
references `Stable` (EntityId);
Where Stable.ID would be the primary key and Stable.EntityId is just another column that I set.
I have a class already that looks like this:
public class ForeignKeyReferenceConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Cascade.All();
//What goes here so that I can change the reference column?
}
}
What do I have to do to get the reference column to change?
As an example, here is what the code for IReferenceConvention looks like to do the same thing:
public class MyReferenceConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.PropertyRef("EntityId");
instance.Cascade.All();
}
}
EDIT:instance.Key.Column("EntityId")
is not the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:这仅在 Fluent NHibernate 下载
IOneToManyInstance
上名为Key
的属性可让您修改关系中使用的键;在该属性上,有一个PropertyRef
方法,这应该就是您正在寻找的方法。There's a property on
IOneToManyInstance
that calledKey
that lets you modify the key used in the relationship; on that property, there's aPropertyRef
method, which should be what you're looking for.