FLUEnt Nhibernate和PostgreSQL中的自动增量

Moc*_*zar 3 c# postgresql nhibernate fluent-nhibernate

Hai,我是Fluent Nhibernate的新手.

我有一个PostgreSQL数据库,我想要的是一个带有自动增量的生成ID.我没有看到PostgreSQL中的功能自动增量,我知道在PostgreSQL中使用自动增量我必须创建一个序列.

除了序列还有另一种方式吗?

如果create sequence是唯一的方法,你能告诉我应该如何绘制它吗?我试图使用它并没有成功:

mapping.Id(x => x.Id, "id").GeneratedBy.Sequence("hibernate-sequence");
Run Code Online (Sandbox Code Playgroud)

hibernate-sequence在使用之前创建过.

问候

Dmi*_*riy 6

奇怪,但是如果你打电话Save(Object)而不是你的代码,插入工作正常SaveOrUpdate(Object)

编辑#1

这对我有用.

实体:

public class Human
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
}

public class HumanMap : ClassMap<Human>
{
    public HumanMap()
    {
        this.Id(x => x.ID).CustomSqlType("Serial").GeneratedBy.Native();
        this.Map(x => x.Name);
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用模式导出进行配置:

static ISessionFactory CreateSF()
{
    return Fluently
    .Configure()
    .Database(PostgreSQLConfiguration.Standard.ConnectionString("Server=127.0.0.1;Port=5432;Database=fnhtest;User=postgres;Password=password"))
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
    .ExposeConfiguration(cfg => { new SchemaExport(cfg).Create(false, true); })
    .BuildSessionFactory(); 
}
Run Code Online (Sandbox Code Playgroud)

  • 不正确,PostgreSQLConfiguration.Standard 不适用于 GeneratedBy.Native(),请使用最新的 PostgreSQLConfiguration.PostgreSQL82(NHibernate 版本:4.0.0.4000)。 (2认同)