1

当使用 SchemaUpdate() 将具有自动递增 id 的映射类导出到 PostgreSQL 服务器时,会引发以下异常: “错误:42P07:关系“seq_wuf”已经存在”。

抛出异常是因为表和序列确实存在于服务器中,但我希望 SchemaUpdate() 忽略已经存在的序列。

映射表

public class CategoryMap : ClassMap<Category>
{
    public static string tableName = "miao";
    public CategoryMap()
    {
        SchemaAction.Export();
        Table(tableName);
        Id(x => x.Id).GeneratedBy.Sequence("SEQ_" + tableName);
        Map(x => x.Name).Column("Category")
                .CustomType("String")
                .Access.Property()
                .Generated.Never()
                //.CustomSqlType("nvarchar(50)")  // <----
                .Not.Nullable()
                .Length(50); ;
        Map(x => x.Description);
    }
}

导出架构:

  FluentConfiguration config = Fluently.Configure();
        config
        .Database(PostgreSQLConfiguration
        .Standard
        .ConnectionString(connStringPosgtgres))
        .Mappings(m => m.FluentMappings.Add(typeof (CategoryMap)))
        .ExposeConfiguration(UpdateSchema)
        .BuildConfiguration();

架构更新()

private static void CreateSchema(Configuration cfg)
    {
        var schemaExport = new SchemaExport(cfg);
        schemaExport.Drop(false, true);
        schemaExport.Create(false, true);
    }     

谢谢,尤瓦尔

4

1 回答 1

0

我发现了为什么会发生这种情况。基本上,虽然 ScemaUpdate 可以创建自动 ID 序列,但它无法检查该序列是否存在于数据库中,因此会尝试重新创建一个现有的对象来提示错误消息。

NHibernate 使用此代码检查序列(src:https ://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Dialect/Schema/AbstractDataBaseSchema.cs )。我试图玩弄它一段时间,也无法从中检索序列。

public virtual DataTable GetTables(string catalog, string schemaPattern, string 
tableNamePattern, string[] types){
var restrictions = new[] { catalog, schemaPattern, tableNamePattern }; 
return connection.GetSchema("Tables", restrictions);\
}

尤瓦尔。

于 2013-01-31T08:53:04.197 回答