简体   繁体   English

NHibernatearchitectureid SQL Server 2014

[英]NHibernate hierarchyid SQL Server 2014

I am using NHibernate with mapping by code, connected an SQL Server. 我正在通过代码映射使用NHibernate,并连接了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. 我为其开发的客户拥有一个SQL Server 2012,我也有相当一段时间了。 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. 由于运行本地服务器的VM最近出现错误,因此我目前正在尝试针对本地主机(即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: 但是,当我现在尝试连接到本地数据库时,NHibernate抛出异常:

Wrong column type in sql_pig_pool.dbo.tbl_Abgabebeleg_Dateien for column path_locator. 列path_locator的sql_pig_pool.dbo.tbl_Abgabebeleg_Dateien中的列类型错误。 Found: hierarchyid, Expected NVARCHAR(255) 找到:architectureid,预期为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. 我认为SQL Server 2012-> 2014中的某些更改,或者工作站上的某种本地配置错误。

UPDATE : I now installed SQL Server 2012 locally and restored my database in there. 更新 :我现在在本地安装了SQL Server 2012,并在那里还原了我的数据库。 The same error as on 2014. So the difference must be related to some local configuration difference. 与2014年相同的错误。因此差异必须与某些本地配置差异有关。

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. 我什至不需要路径定位器(我通过stream_id访问文件),但这是PK,所以很重要。 NHibernate doesn't like a missing Id property in its mapping. NHibernate不喜欢映射中缺少Id属性。

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. 我已经看过GitHub-Project( NHibernate.HierarchyId ),但这是用于流畅的映射,而不是用于代码映射。 I cannot simply use the string "hierarchyid" in my mapping for the type. 我不能在类型的映射中简单地使用字符串“ hierarchyid”。 My personal tries with building an IUserType-derived class also failed miserably. 我个人尝试构建IUserType派生的类也失败了。

Addendum: I don't have a subfolder "Indexes" in SQL Server Management Studio anymore on my local DB. 附录:我的本地数据库上不再有SQL Server Management Studio的子文件夹“索引”。 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). 它仍然可以在我的客户数据库上使用(具有3个索引:path_locator上的PK,stream_id上的UQ,parent_path_locator上的UQ +名称)。 Is this relevant in any kind? 这有任何关系吗? UPDATE In my new 2012 instance I have this Indexes folder available again. 更新在新的2012年实例中,此索引文件夹再次可用。

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. 在我的配置IDbIntegrationConfigurationProperties ,我没有db.SchemaAction我的原始版本定义,但db.SchemaAction = SchemaAutoAction.Validate我现在的版本。

Simply removing the validation led to a fully working solution again. 只需删除验证即可获得完整的解决方案。

Hours upon hours wasted for this dumb error. 这个愚蠢的错误浪费了数小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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