2017-08-09 34 views
0

以下是我的mssql数据库脚本 我在此部门创建表部门是主键,但不是自动递增,并且数据类型是varchar。我们是否可以避免使用NHibernate在.hbm.xml映射文件中的主键ID列的自动增量

create table Department(
    deptid varchar(30) NOT NULL CONSTRAINT deptid_P_KEY PRIMARY KEY, 
    departmentname varchar(100) 
    ) 

以下是我的映射文件。

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping assembly="TestDal" namespace="NHSample.TestDal.Dal" xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="DepartmentEntity" table="Department" lazy="true" > 
    <id name="DeptId" > 
     <generator class="assigned" /> 
     <column name="deptid" sql-type="varchar" not-null="true" /> 
    </id> 
    <property name="Departmentname"> 
     <column name="departmentname" sql-type="varchar" not-null="false" /> 
    </property> 
    </class> 
    </hibernate-mapping> 

当我运行,并在配置连接字符串路径的时间,然后提示错误 -

XML validation error: The 'type' attribute is not declared. 

以下是我的配置代码:

static Configuration nhConfiguration; 
static ISessionFactory nhSessionFactory; 
internal static void CreateSessionFactory(string configFilePath) 
{ 
    nhConfiguration = new Configuration(); 
    try 
    { 
     if (string.IsNullOrEmpty(configFilePath)) 
      nhConfiguration.Configure(); 
     else 
      nhConfiguration.Configure(configFilePath); 

     nhConfiguration.SessionFactory().DefaultFlushMode(flushMode); 
    } 
    catch (Exception exception) 
    { 
     throw new NHException("Failed to configure session factory.", exception); 
    } 
    try 
    { 
     nhSessionFactory = nhConfiguration.BuildSessionFactory(); 
    } 
} 

在上面的代码以下行给出的错误: nhConfiguration.Configure(configFilePath);

那么如何使用nhibernate来做到这一点。

回答

1

我想你可能需要改变你的id映射:

<id name="DeptId" generator="assigned" column="deptid" type="string"/> 

type属性是可选的,如果底层属性是相同的类型可以被省略。

+0

感谢大卫这个解决方案在我的应用程序中正常工作。 –

相关问题