Nhibernate:麻烦映射一对多的关系

时间:2012-01-26 11:54:22

标签: c# asp.net .net nhibernate nhibernate-mapping

我对nhibernate非常非常新,而且我确定有很多明显的东西我会在这里忽略,但我一直在寻找答案。现在几天,根本无法找到适合我特定问题的解决方案。我真的非常感谢你的帮助。

我首先描述我的问题,然后发布代码:

在我的对象模型中,我有一个"事件"与一个IList"组织者"。我想使用nhibernate,以便当我执行" session.Save(anEvent)"时,数据被写入数据库中的三个表:Events,Organizers和Organizers_Events(连接表)。当我使用下面的代码时,我收到以下错误:

Invalid column name 'EventID'

我的代码: 首先是.hbm文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BLL"
                   namespace="BusinessLogic">

  <class name="Event" table="Events">
    <id name="EventID">
      <column name="ID"/>
      <generator class="native" />
    </id>
    <property name="Name">
      <column name="Name"/>
    </property>
    <bag name="Organizers" table="Organizers_Events" inverse="false" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >
      <key column="EventID"/>
      <one-to-many class="Organizer" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BLL"
                   namespace="BusinessLogic">

  <class name="Organizer" table="Organizers">
    <id name="Id">
      <column name="ID"/>
      <generator class="native" />
    </id>
    <property name="FullName">
      <column name="FullName"/>
    </property>
    <many-to-one name="TheEvent" column="EventID" />
  </class>
</hibernate-mapping>

接下来是对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Event
/// </summary>
/// 

namespace BusinessLogic
{
    public class Event
    {
        private int eventID;

        public virtual int EventID
        {
            get { return eventID; }
            set { eventID = value; }
        }
        private IList<Organizer> _organizers = null;

        public virtual IList<Organizer> Organizers
        {
            get { return _organizers; }
            set { _organizers = value; }
        }
        public Event()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogic
{
    public class Organizer:Invitee
    {
        private Event theEvent = new Event();

        public virtual Event TheEvent
        {
            get { return theEvent; }
            set { theEvent = value; }
        }
    }
}

最后,数据库架构:

CREATE TABLE [dbo].[Events](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Organizers](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FullName] [nvarchar](550) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Organizers_Events](
    [OrganizerID] [int] NOT NULL,
    [EventID] [int] NOT NULL
) ON [PRIMARY]

我已删除不相关的数据以简化问题。我认为其他任何东西都不会影响我的问题。出于我们的目的,我只希望Event对象将它的EventID和Name写入Events表,IList中的每个组织器对象将其ID和FullName写入Organizers表,以及EventID和Organizer&# 39;要写入Organizers_Events表的ID。

我做错了什么?如果它很多,请告诉我。或者,如果我已经错过了在线的答案,请指导我。

2 个答案:

答案 0 :(得分:3)

在一对多关联中,您不需要加入表格。只有在多对多的配置中才需要连接表。正确的映射

<bag name="Organizers" inverse="false" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >
    <key column="EventID"/>
    <one-to-many class="Organizer" />
</bag>

更正一对多关联的数据库架构

CREATE TABLE [dbo].[Events]
(
  [ID] [int] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar](max) NOT NULL
)
 ON [PRIMARY] 

CREATE TABLE [dbo].[Organizers]
( 
 [ID] [int] IDENTITY(1,1) NOT NULL, 
 [FullName] [nvarchar](550) NULL,
 [EventID] [int] NOT NULL // add FK constraint here
) 
ON [PRIMARY]   

答案 1 :(得分:0)

我想你忘记在你的行李声明中加上inverse =“false”:

<bag name="Organizers" inverse="false" table="Organizers_Events" cascade="all-delete-orphan" lazy="true" access="nosetter.camelcase-underscore" >