NHibernate 映射错误与 v4.0
NHibernate Mapping Error with v4.0
我正在使用 VS 2013 32 位,nHibernate v4.0.0.4000。
我是 nHiberNate 的新手,无论如何我可以避免在不使用流利的情况下创建映射。我需要很好的工作样本,但我目前找不到。
我收到以下代码的错误。
Additional information: Could not compile the mapping document: C:\Project...\Some\Models\NHibernate\Mappings\SomeInfo.hbm.xml
InnerException: {"Problem trying to set property type by reflection"}
代码与映射 -----
My class:
namespace Some.Models
{
public class SomeInfo
{
public long Form_Id { get; set; }
public string Account_Id { get; set; }
public char Entity { get; set; }
public string Name { get; set; }
public decimal Ownership_Percent { get; set; }
}
}
映射实例创建:
namespace Some.Models
{
public class Hibernate_Connection
{
ISessionFactory sessionFactory;
ISession OpenSession()
{
if (sessionFactory == null)
{
var cgf = new Configuration();
var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Configuration\hibernate.cfg.xml"));
cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Mappings")));
sessionFactory = data.BuildSessionFactory();
}
return sessionFactory.OpenSession();
}
映射:Models/NHiberNate/Mappings/SomeInfo.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="Some"
namespace="Some.Models"
auto-import="true">
<class name="SomeInfo" table="Some_Table" lazy="false">
<!--<id name="ID" column="Form_Id" Type="Long">
<generator class="native"></generator> </id>-->
<id name="ID" column="Form_Id">
<generator class="native"></generator>
</id>
<property name="Form_Id" column="Form_Id"></property>
<property name="Account_Id" column="Account_Id"></property>
<property name="Entity" column="Entity"></property>
<property name="Name" column="Name"></property>
<property name="Ownership_Percent" access="property"
column="Ownership_Percent" type="Decimal"></property>
</class>
</hibernate-mapping>
您的模型属性需要是虚拟的:
public class SomeInfo
{
public virtual long Form_Id { get; set; }
public virtual string Account_Id { get; set; }
public virtual char Entity { get; set; }
public virtual string Name { get; set; }
public virtual decimal Ownership_Percent { get; set; }
}
我想如果您提供完整的异常,我们将看到 (在堆栈跟踪中) 消息如下:
... Could not find a getter for property 'ID'
in class 'Some.Models.SomeInfo' ...
因为映射指示 NHibernate 映射 C# 属性 ID
<id name="ID" column="Form_Id">
<generator class="native"></generator>
</id>
并且没有属性ID。此外,还有同一列的第二个映射 Form_Id
<property name="Form_Id" column="Form_Id"></property>
这不应该是这样的。我们应该将 one 列映射到 one 属性。所以,这个映射应该有效
<class name="SomeInfo" table="Some_Table" lazy="false">
<id name="Form_Id" column="Form_Id">
<generator class="native"></generator>
</id>
<property name="Account_Id" column="Account_Id"></property>
...
</class>
我正在使用 VS 2013 32 位,nHibernate v4.0.0.4000。 我是 nHiberNate 的新手,无论如何我可以避免在不使用流利的情况下创建映射。我需要很好的工作样本,但我目前找不到。
我收到以下代码的错误。
Additional information: Could not compile the mapping document: C:\Project...\Some\Models\NHibernate\Mappings\SomeInfo.hbm.xml
InnerException: {"Problem trying to set property type by reflection"}
代码与映射 -----
My class:
namespace Some.Models
{
public class SomeInfo
{
public long Form_Id { get; set; }
public string Account_Id { get; set; }
public char Entity { get; set; }
public string Name { get; set; }
public decimal Ownership_Percent { get; set; }
}
}
映射实例创建:
namespace Some.Models
{
public class Hibernate_Connection
{
ISessionFactory sessionFactory;
ISession OpenSession()
{
if (sessionFactory == null)
{
var cgf = new Configuration();
var data = cgf.Configure(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Configuration\hibernate.cfg.xml"));
cgf.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"Models\NHibernate\Mappings")));
sessionFactory = data.BuildSessionFactory();
}
return sessionFactory.OpenSession();
}
映射:Models/NHiberNate/Mappings/SomeInfo.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="Some"
namespace="Some.Models"
auto-import="true">
<class name="SomeInfo" table="Some_Table" lazy="false">
<!--<id name="ID" column="Form_Id" Type="Long">
<generator class="native"></generator> </id>-->
<id name="ID" column="Form_Id">
<generator class="native"></generator>
</id>
<property name="Form_Id" column="Form_Id"></property>
<property name="Account_Id" column="Account_Id"></property>
<property name="Entity" column="Entity"></property>
<property name="Name" column="Name"></property>
<property name="Ownership_Percent" access="property"
column="Ownership_Percent" type="Decimal"></property>
</class>
</hibernate-mapping>
您的模型属性需要是虚拟的:
public class SomeInfo
{
public virtual long Form_Id { get; set; }
public virtual string Account_Id { get; set; }
public virtual char Entity { get; set; }
public virtual string Name { get; set; }
public virtual decimal Ownership_Percent { get; set; }
}
我想如果您提供完整的异常,我们将看到 (在堆栈跟踪中) 消息如下:
... Could not find a getter for property
'ID'
in class 'Some.Models.SomeInfo' ...
因为映射指示 NHibernate 映射 C# 属性 ID
<id name="ID" column="Form_Id">
<generator class="native"></generator>
</id>
并且没有属性ID。此外,还有同一列的第二个映射 Form_Id
<property name="Form_Id" column="Form_Id"></property>
这不应该是这样的。我们应该将 one 列映射到 one 属性。所以,这个映射应该有效
<class name="SomeInfo" table="Some_Table" lazy="false">
<id name="Form_Id" column="Form_Id">
<generator class="native"></generator>
</id>
<property name="Account_Id" column="Account_Id"></property>
...
</class>