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

发布于 2024-08-14 04:06:29 字数 2552 浏览 4 评论 0原文

使用 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; }
}

一家公司可以拥有多个帐户。一般来说,我会直接获得一个帐户,然后偶尔想找到关联的公司,因此不需要公司模型上的帐户列表。

Fluent NHibernate 创建以下 hbms:

公司

<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>

帐户

<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>

我收到以下错误:

命名空间中的元素“class” 'urn:nhibernate-mapping-2.2' 有 无效的子元素“多对一” 名称空间 '瓮:nhibernate-mapping-2.2'。名单 预期可能的元素:'元, 子选择、缓存、同步、 评论、tuplizer、id、复合 id' 在命名空间中 '瓮:nhibernate-mapping-2.2'。

我需要做什么才能使其成为有效的映射?

Using Fluent NHibernate with automapping, I am trying to map the following domain:

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:

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'. List of
possible elements expected: 'meta,
subselect, cache, synchronize,
comment, tuplizer, id, composite-id'
in namespace
'urn:nhibernate-mapping-2.2'.

What do I need to do to make this a valid mapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-08-21 04:06:29

事实证明,问题不在于这些域类。我在同一个程序集中有一个 Repository 类,Fluent NHibernate 显然试图将其包含在域中。正是尝试映射此类导致了错误(不幸的是,错误消息没有说明是哪种类型导致了问题)。

我通过在命名空间上添加 where 约束来修复它。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文