简体   繁体   English

NHibernate映射错误-无效的子元素“多对一”

[英]NHibernate mapping error - invalid child element 'many-to-one'

Using Fluent NHibernate with automapping, I am trying to map the following domain: 将Fluent NHibernate与自动映射结合使用,我正在尝试映射以下域:

public class Company: IModel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class Account: IModel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }
}

One Company can have many accounts. 一个公司可以有多个帐户。 In general I will get an account directly and then occasionally want to find the associated Company, so there's no need for a list of accounts on the company model. 通常,我将直接获得一个帐户,然后偶尔要查找关联的公司,因此不需要公司模型上的帐户列表。

Fluent NHibernate creates the following hbms: 流利的NHibernate创建以下hbms:

Company 公司

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="DataModel.Company, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Company`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
  </class>
</hibernate-mapping>

Account 帐户

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="DataModel.Account, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Account`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
    <many-to-one class="DataModel.Company, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Company">
      <column name="Company_id" />
    </many-to-one>
  </class>
</hibernate-mapping>

And I am getting the following error: 我收到以下错误:

The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'many-to-one' in namespace 'urn:nhibernate-mapping-2.2'. 名称空间“ urn:nhibernate-mapping-2.2”中的元素“ class”在名称空间“ urn:nhibernate-mapping-2.2”中具有无效的子元素“多对一”。 List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'. 预期的可能元素列表:名称空间“ urn:nhibernate-mapping-2.2”中的“元,子选择,缓存,同步,注释,tuplizer,id,复合ID”。

What do I need to do to make this a valid mapping? 我需要做什么才能使其成为有效的映射?

The problem turned out not to be with these domain classes. 问题出在这些域类之外。 I had a Repository class in the same assembly, which Fluent NHibernate was apparently trying to include in the domain. 我在同一程序集中有一个Repository类,Fluent NHibernate显然试图将其包含在域中。 It was the attempted mapping of this class which caused the error (unfortunately the error message didn't say which type caused the problem). 正是此类的尝试映射导致了错误(不幸的是,错误消息没有说明是哪种类型引起了问题)。

I fixed it by adding a where constraint on the namespace. 我通过在名称空间上添加where约束来修复它。

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

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