我正在尝试构建MusicStore Nhibernate端口,我遇到了这个错误:
无法在课程中找到属性'OrderInfo'的getter 'MvcMusicStore.Models.OrderDetail'
具有以下内部异常:
NHibernate.PropertyNotFoundException:无法找到getter 类'MvcMusicStore.Models.OrderDetail'中的属性'OrderInfo'
使用OrderDetail的以下映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MvcMusicStore" namespace="MvcMusicStore.Models">
<class name="OrderDetail">
<id name="Id">
<generator class="hilo" />
</id>
<many-to-one name="OrderInfo" column="OrderInfoId" />
<property name="Quantity" />
<property name="UnitPrice" />
<many-to-one name="Album" column="AlbumId" />
</class>
</hibernate-mapping>
C#类定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.Models
{
public class OrderDetail : Entity
{
public virtual OrderInfo Order { get; set; }
public virtual Album Album { get; set; }
public virtual int Quantity { get; set; }
public virtual decimal UnitPrice { get; set; }
}
}
OrderInfo类的映射如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MvcMusicStore" namespace="MvcMusicStore.Models">
<class name="OrderInfo">
<id name="Id">
<generator class="hilo" />
</id>
<property name="OrderDate" />
<property name="Username" />
<property name="FirstName" />
<property name="LastName" />
<property name="Address" />
<property name="City" />
<property name="State" />
<property name="PostalCode" />
<property name="Country" />
<property name="Phone" />
<property name="Email" />
<property name="Total" />
<set name="OrderDetails">
<key column="OrderInfoId" />
<one-to-many class="OrderDetail" />
</set>
</class>
</hibernate-mapping>
它具有以下C#类定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcMusicStore.Models
{
public class OrderInfo : Entity
{
public virtual DateTime OrderDate { get; set; }
public virtual string Username { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string PostalCode { get; set; }
public virtual string Country { get; set; }
public virtual string Phone { get; set; }
public virtual string Email { get; set; }
public virtual string Total { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
}
Entity类是一个添加Guid ID属性的抽象类。我究竟做错了什么?老实说,我的映射没有任何错误。
答案 0 :(得分:6)
在映射文件中,您说:
<many-to-one name="OrderInfo" column="OrderInfoId" />
你必须说:
<many-to-one name="Order" column="OrderInfoId" />
答案 1 :(得分:3)
属性名称为Order
,而非OrderInfo
。将属性重命名为OrderInfo
:
public virtual OrderInfo OrderInfo {get;set;}
或更改配置:
<many-to-one name="Order" column="OrderInfoId" />