我在我的应用程序中使用流畅的 NHibernate。我想使用我的映射从代码创建数据库表。所有表均已创建,但外键列已更改。我的用户类映射为
HasMany(Function(x) x.Friends).ForeignKeyConstraintName("UserID").Inverse()
还有 friend 类
References(Function(x) x.Users).Column("UserID")
但是在 friend 表中创建的列是Users_id。但我明确定义为UserID。我究竟做错了什么。请帮忙
最佳答案
我们需要的是引入自定义约定。请阅读以下内容以了解更多详细信息:
- Fluent NHibernate conventions – examples (作者:Marcin Obel)
摘录:
使用默认 Fluent-NHibernate 设置生成的数据库(在示例中,但与我们的情况类似)如下所示:
create table `Athlete` (
...
Country_id INTEGER,
...
)
然后,介绍了这个公约:
ForeignKeyNameConvention – says that name of every column containing foreign key id should consist of name of type which it points to with “Id” suffix.
public class ForeignKeyNameConvention : IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.Column(instance.EntityType.Name + "Id");
}
}
生成的 SQL 脚本:
create table Athletes (
...
CountryId INTEGER,
...
关于c# - 在 Fluent NHibernate 中创建数据库期间列名发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24502517/