0

我对 Nhibernate 很陌生。我想通过使用 NHibernate 作为映射数据库工具在 asp.net 中开发一个应用程序。

我有以下表格模式:

CREATE TABLE [dbo].[tblTeam](
    [TeamID] [int] IDENTITY(1,1) NOT NULL,
    [TeamName] [varchar](100) NOT NULL
)


CREATE TABLE [dbo].[tblEmployee](
    [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
    [EmployeeName] [varchar](100) NOT NULL
)

CREATE TABLE [dbo].[tblTeamEmployee](
    [TeamID] [int] NOT NULL,
    [EmployeeID] [int] NOT NULL
)

这是我制作的 Nhibernate 映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Team, TelDir.Core" table="tblTeam" lazy="false">
    <id name="ID" column="TeamID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="TeamName" column="TeamName" />

    <bag name="Employees" cascade="none" table="tblTeamEmployee" lazy="false" access="readonly">
      <key column="EmployeeID"/>
      <many-to-many class="TelDir.Core.Domain.Employee, TelDir.Core" column="TeamID"/>
    </bag>

  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Employee, TelDir.Core" table="tblEmployee" lazy="false">
    <id name="ID" column="EmployeeID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="EmployeeName" column="EmployeeName" />        

    <bag name="Teams" cascade="none" table="tblTeamEmployee" lazy="false" >
      <key column="EmployeeID"/>
      <many-to-many class="TelDir.Core.Domain.Team, TelDir.Core" column="EmployeeID"/>
    </bag>

  </class>
</hibernate-mapping>

我的 POCO 类定义如下:

namespace TelDir.Core.Domain
{
    public class Team   {

        private string _TeamName = "";
        private IList<Employee> _employees = new List<Employee>();

        public Team() { }

        public  string TeamName {
            get { return _TeamName }
            set { _TeamName = value; }
        }


        public  IList<Employee> Employees
        {
            get { return new List<Employee>(_employees).AsReadOnly(); }
            protected set { _employees = value; }          
        }

        public  void AddEmployee(Employee em)
        {
            if (!_employees.Contains(em)){
                _employees.Add(em);
            }
        }

        public  void RemoveEmployee(Employee em)
        {
            if (_employees.Contains(em)){
                _employees.Remove(em);
            }
        }  
    }
}




namespace TelDir.Core.Domain
{
    public class Employee  {

        private string _EmployeeName = "";
        private IList<Team> _teams = new List<Team>();

        public Employee() { }

        public  string EmployeeName {
            get { return _EmployeeName}
            set { _EmployeeName = value; }
        }


        public  IList<Team> Teams
        {
            get { return new List<Team>(_teams).AsReadOnly(); }
            protected set { _teams = value; }          
        }

        public  void AddTeam(Team tm)
        {
            if (!_teams.Contains(tm)){
                _teams.Add(tm);
            }
        }

        public  void RemoveTeam(Team tm)
        {
            if (_teams.Contains(tm)){
                _teams.Remove(tm);
            }
        }  
    }
}

我不确定我的映射和实体类是否定义明确?

假设我的表中有以下数据

tblTeam
        TeamID     |      TeamName
        --------------------------
          1        |         A
        --------------------------
          2        |         B

tblEmployee
        EmployeeID |      EmployeeName
        ------------------------------
          1        |      Jhon
        ------------------------------
          2        |      Michel
        ------------------------------
          3        |      Lino

tblTeamEmployee
        TeamID     |      EmployeeID
        ------------------------------
          1        |      1
        ------------------------------
          1        |      2
        ------------------------------
          2        |      3
        ------------------------------
          2        |      1        

如何从 ASP.NET 页面中的 Team='B' 中删除 Employee name='Jhon'?

4

1 回答 1

2

当您想更改您的收藏时,请.AsReadOnly()从中删除。

然后,如果您同时拥有(1 和 2)的 id,只需执行以下操作:

var teamB = session.Get<Team>(2);
teamB.Employees.Remove(teamB);
session.Flush();
于 2012-06-08T03:31:08.330 回答