问题
I am very new to Nhibernate. I would like to develop an application in asp.net by using NHibernate as mapping database tools.
I have following tables schemas:
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
)
And here is Nhibernate mapping file that I have made:
<?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>
And my POCO class is defined as below:
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);
}
}
}
}
I am not sure whether my mapping and entity class is well defined?
Suppose I have following data in my table
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
How can I remove Employee name='Jhon' from Team='B' in ASP.NET page?
回答1:
As you want to change your collection, remove the .AsReadOnly()
from it.
Then, if you have the ids of both (1 and 2), just do:
var teamB = session.Get<Team>(2);
teamB.Employees.Remove(teamB);
session.Flush();
来源:https://stackoverflow.com/questions/10942711/nhibernate-many-to-many-relationship-mapping