我对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'?
答案 0 :(得分:2)
如果您想更改收藏集,请从中删除.AsReadOnly()
。
然后,如果你有两个(1和2)的ID,只需执行:
var teamB = session.Get<Team>(2);
teamB.Employees.Remove(teamB);
session.Flush();