0

我有这些表:

create table Person
(
PersonId int identity(1,1) primary key,
PersonName nvarchar(100) not null
);

create table Question
(
QuestionId int identity(1,1) primary key,
QuestionText nvarchar(100) not null,
AskedBy_PersonId int not null references Person(PersonId),
QuestionModifiedBy_PersonId int not null references Person(PersonId)
);

我有这些模型:

public class Question
{
    public virtual int QuestionId { get; set; }

    public virtual string QuestionText { get; set; }

    public virtual Person AskedBy { get; set; }
    public virtual Person QuestionModifiedBy { get; set; }
}

public class Person
{
    public virtual int PersonId { get; set; }
    public virtual string PersonName { get; set; }
}

我正在使用 FluentNHibernate 的自动映射,引用属性默认为这些数据库列名:

AskedBy_id
QuestionModifiedBy_id

如何让 FluentNHibernate 使引用属性映射到这种样式的外部列名?

AskedBy_PersonId
QuestionModifiedBy_PersonId

截至目前,我正在手动覆盖:

.Override<Question>(x => 
{
    x.References(y => y.AskedBy).Column("AskedBy_PersonId");
    x.References(y => y.QuestionModifiedBy).Column("QuestionModifiedBy_PersonId");
})

我想删除该覆盖并希望 Fluent NHibernate 自动使外部列名遵循上面的命名模式

如何使用 Fluent NHibernate 实现这一目标?

4

1 回答 1

1

使用IReferenceConvention实现应该很容易:

public class ReferenceConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(
            instance.Name + "_" + instance.Property.PropertyType.Name + "Id");
    }
}

NHibernate 应该配置为读取约定(使用类似这样的东西):

Fluently.Configure()
    //... other configuration
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<Person>()
            .Conventions.AddFromAssemblyOf<ReferenceConvention>());
于 2012-08-21T13:42:31.060 回答