C# NHibernate:表X中的关联引用未映射的类:X
我有两个订单和客户对象的映射文件C# NHibernate:表X中的关联引用未映射的类:X,c#,nhibernate,nhibernate-mapping,C#,Nhibernate,Nhibernate Mapping,我有两个订单和客户对象的映射文件 <class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers"> <id name="ID" column="customer_id"> <generator class="guid" /> </id> <property name="FirstName" column="first_
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
客户映射:
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
订单映射:
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OODB.Domain" namespace="OODB.Domain">
<class name="OODB.Domain.CustomerOrder, OODB.Domain" entity-name="Orders">
<id name="ID" column="order_id">
<generator class="guid"></generator>
</id>
<property name="OrderNo" column="order_no" length="8" not-null="true"></property>
<property name="Status" column="status" not-null="true"></property>
<many-to-one name="Orderer" class="Customer" column="customer_id" insert="true" not-found="exception" fetch="join"/>
</class>
</hibernate-mapping>
客户类别:
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
namespace OODB.Domain
{
public class Customer : ModelBase<Customer>
{
public virtual string FirstName
{
get;
set;
}
public virtual string LastName
{
get;
set;
}
public virtual string EMail
{
get;
set;
}
public virtual string Telephone
{
get;
set;
}
public virtual Address Address
{
get;
set;
}
...
}
名称空间OODB.Domain
{
公共类客户:模型库
{
公共虚拟字符串名
{
得到;
设置
}
公共虚拟字符串LastName
{
得到;
设置
}
公共虚拟字符串电子邮件
{
得到;
设置
}
公共虚拟字符串电话
{
得到;
设置
}
公共虚拟地址
{
得到;
设置
}
...
}
客户订单类别:
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
public class CustomerOrder : ModelBase<CustomerOrder>
{
public virtual string OrderNo
{
get;
set;
}
public virtual Customer Orderer
{
get;
set;
}
public virtual OrderStatus Status
{
get;
set;
}
...
}
public类客户订单:ModelBase
{
公共虚拟字符串序号
{
得到;
设置
}
公共虚拟客户订购者
{
得到;
设置
}
公共虚拟订单状态
{
得到;
设置
}
...
}
如果我删除多对一的内容(映射被集成为一个嵌入式资源。我已经检查了两次)。否则我会得到错误“来自表Orders的关联引用了一个未映射的类:OODB.Domain.Customer”。但是客户对象被映射了…我缺少了什么?我猜对于您的示例来说:
多对一
元素中的“类”规范必须与其他所有类
规范具有相同的详细程度。尤其是,必须包括部件
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
<many-to-one name="Orderer" class="OODB.Domain.Customer, OODB.Domain" column="customer_id" insert="true" not-found="exception" fetch="join"/>
我混淆了class元素上的table和entity name属性。\此异常的一个可能原因是.html文件不是“嵌入式资源”。如果映射文件(hbm)不在正确的目录中,则会出现此异常。如果查看输出窗口,您将看到如下内容:
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
NHibernate.Cfg.Configuration: ERROR lambda_method - An association from the table
SubsequentReportDetail refers to an unmapped class: NS.Project.Data.Domain.Class
NHibernate.MappingException: An association from the table X refers to an
unmapped class: NS.Project.Data.Domain.Class
我的问题是..同一个属性映射了两次
<class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
<id name="ID" column="customer_id">
<generator class="guid" />
</id>
<property name="FirstName" column="first_name"/>
<property name="LastName" column="last_name"/>
<property name="EMail" column="email"/>
<property name="Telephone" column="telephone" />
<component name="Address" class="Address">
<property name="Street" column="street"></property>
<property name="PostalCode" column="postal_code"></property>
<property name="City" column="city"></property>
</component>
</class>
</hibernate-mapping>
Property(x => x.MyProperty);
Bag(x => x.MyProperty, colmap => { }, map => map.OneToMany(x => { }));
可能会向我们展示“CustomerOrder”类的实际类定义。特别是“Orderer”属性。另一个要测试的东西:您是否将“Customer.hbm.xml”上的“BuildAction”设置为“EmbeddedResource”?(这是一件简单的事情,过去我一直在做)您是否自行获取了客户对象?您是否对客户映射中的Address类进行了相同的调整?您应该查看Fluent nHibernate。它提供了一种更不容易出错的定义映射的方法。我需要在.hbm文件的properties build操作中将其设置为嵌入式资源