未使用主键约定

时间:2011-10-20 08:15:41

标签: .net fluent-nhibernate

我有以下Fluent NHibernate配置:

var fluentConfig = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
    .ConnectionString(c => c
    .FromConnectionStringWithKey("TestPortalDbContext"))
)
.Mappings(m =>
    m.FluentMappings.AddFromAssemblyOf<Program>()
    .Conventions.AddFromAssemblyOf<PrimaryKeyConvention>()
    .Conventions.Add<PrimaryKeyConvention>()
);

问题在于未使用PrimaryKeyConvention。我的配置不正确吗?

更新

我的一个映射(由NHibernate Mapping Generator生成)

public class TestResultMap : ClassMap<TestResult> {

    public TestResultMap() {
        Table("TestResults");
        LazyLoad();
        Id(x => x.Id).GeneratedBy.Identity().Column("Id");
        References(x => x.TestSession).Column("SessionId");
        References(x => x.Instruction).Column("InstructionId");
        References(x => x.SolvedBy).Column("SolvedBy");
        Map(x => x.State).Column("StateId").CustomType<TestState>();
        Map(x => x.ActualResult).Column("ActualResult");
        Map(x => x.CompletedAt).Column("CompletedAt");
        Map(x => x.IsSolved).Not.Nullable().Column("IsSolved");
    }
}

错误

我得到的错误是找不到密钥“ClassName_id”。

惯例

public class PrimaryKeyConvention
  : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + "Id");
    }
}

它在查找相关实体时抱怨PK

1 个答案:

答案 0 :(得分:0)

约定也适用于流畅的映射,但从不覆盖明确设置的值,如.Column("Id")