简体   繁体   中英

NHibernate hierarchyid SQL Server 2014

I am using NHibernate with mapping by code, connected an SQL Server. This server also contains file tables.

The customer, for whom I develop, has an SQL Server 2012, which I also had for quite a while. Due to a recent error in the VM running my local server, I'm currently trying to run my code against a database on localhost, which is SQL Server 2014.

I received a backup from the customers DB and restored it into my local server.

Before everything went fine, my code ran without any big problems. But when I now try to connect to my local DB, NHibernate throws an Exception:

Wrong column type in sql_pig_pool.dbo.tbl_Abgabebeleg_Dateien for column path_locator. Found: hierarchyid, Expected NVARCHAR(255)

When I change my connection string to the customer DB, everything works again. I assume that something has changed in either SQL Server 2012->2014, or some kind of local configuration on my workstation is wrong.

UPDATE : I now installed SQL Server 2012 locally and restored my database in there. The same error as on 2014. So the difference must be related to some local configuration difference.

My class:

public class TblAbgabebelegDateien
{
    public TblAbgabebelegDateien() { stream_id = Guid.NewGuid(); }
    public virtual string path_locator { get; set; }
    public virtual TblAbgabebelegDateien tbl_AbgabebelegDateienVal { get; set; }
    public virtual Guid stream_id { get; set; }
    public virtual byte[] file_stream { get; set; }
    public virtual string name { get; set; }
    public virtual string file_type { get; set; }
    public virtual long? cached_file_size { get; set; }
    public virtual string creation_time { get; set; }
    public virtual string last_write_time { get; set; }
    public virtual string last_access_time { get; set; }
    public virtual bool is_directory { get; set; }
    public virtual bool is_offline { get; set; }
    public virtual bool is_hidden { get; set; }
    public virtual bool is_readonly { get; set; }
    public virtual bool is_archive { get; set; }
    public virtual bool is_system { get; set; }
    public virtual bool is_temporary { get; set; }
}

My mapping:

public class TblAbgabebelegDateienMap : ClassMapping<TblAbgabebelegDateien> {

    public TblAbgabebelegDateienMap() {
        Schema("dbo");
        Table("tbl_Abgabebeleg_Dateien");
        Lazy(true);
        Id(x => x.path_locator, map => map.Generator(Generators.Assigned));
        Property(x => x.stream_id, map => { map.NotNullable(true); map.Unique(true);  });
        Property(x => x.file_stream);
        Property(x => x.name, map => 
        {
            map.NotNullable(true);
            map.Unique(true);
            map.Length(255);
        });
        Property(x => x.file_type, map => map.Length(255));
        Property(x => x.cached_file_size, map => map.Precision(19));
        Property(x => x.creation_time, map => map.NotNullable(true));
        Property(x => x.last_write_time, map => map.NotNullable(true));
        Property(x => x.last_access_time);
        Property(x => x.is_directory, map => map.NotNullable(true));
        Property(x => x.is_offline, map => map.NotNullable(true));
        Property(x => x.is_hidden, map => map.NotNullable(true));
        Property(x => x.is_readonly, map => map.NotNullable(true));
        Property(x => x.is_archive, map => map.NotNullable(true));
        Property(x => x.is_system, map => map.NotNullable(true));
        Property(x => x.is_temporary, map => map.NotNullable(true));
        ManyToOne(x => x.tbl_AbgabebelegDateienVal, map => 
        {
            map.Column("parent_path_locator");
            map.PropertyRef("path_locator");
            map.Cascade(Cascade.None);
        });
    }
}

Does anyone have an idea what I should change, so that I can use those tables again? I don't even need the path locator (I access the files via the stream_id), but it is the PK, so kinda important. NHibernate doesn't like a missing Id property in its mapping.

I only read those tables, so any configuration which lets me access the file table is welcome.

I've seen a GitHub-Project ( NHibernate.HierarchyId ), but it is for fluent mapping, not for mapping by code. I cannot simply use the string "hierarchyid" in my mapping for the type. My personal tries with building an IUserType-derived class also failed miserably.

Addendum: I don't have a subfolder "Indexes" in SQL Server Management Studio anymore on my local DB. It is still available on my customers DB (with 3 indexes: PK on path_locator, UQ on stream_id, UQ on parent_path_locator + name). Is this relevant in any kind? UPDATE In my new 2012 instance I have this Indexes folder available again.

It all boiled down to one crucial difference:

In my configuration of the IDbIntegrationConfigurationProperties , I had no db.SchemaAction defined for my original version, but db.SchemaAction = SchemaAutoAction.Validate for my current version.

Simply removing the validation led to a fully working solution again.

Hours upon hours wasted for this dumb error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM