Fluent-Nhibernate 多对多关系问题
我在尝试使用 Fluent Nhibernate 删除多对多关系时遇到问题。我有以下域类:
public class Organisation
{
public virtual int Id {get; set;}
private IList<OrganisationRelationshiop> relatedOrganisations;
public virtual IList<OrganisationRelationship> RelatedOrganisation
{
get
{
return this.relatedOrganisations;
}
protected set
{
this.relatedOrganisations = value;
}
}
public virtual void RemoveRelatedOrganisation(OrganisationRelationship organisationRelationship)
{
this.relatedOrganisations.Remove(organisationRelationship);
}
}
这是我的 OrganizationRelationship 类,它代表组织之间的多对多关系。
public class OrganisationRelationship
{
public virtual int Id {get; set;}
public virtual Organisation Organisation{ get; set; }
public virtual OrganisationRelationshipType OrganisationRelationshipType { get; set; }
public virtual Organisation RelatedOrganisation { get; set; }
}
她是表的脚本:
组织表:组织
CREATE TABLE [dbo].[Organisation](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrganisationName] [nvarchar](200) NOT NULL,
CONSTRAINT [PK_Organisation] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
关系商店表:
CREATE TABLE [dbo].[OrganisationRelationship](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrganisationId] [int] NOT NULL,
[RelatedOrganisationId] [int] NOT NULL,
[OrganisationRelationshipTypeId] [int] NOT NULL,
CONSTRAINT [PK_OrganisationRelationship] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
组织关系类型表:
CREATE TABLE [dbo].[OrganisationRelationshipType](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_OrganisationRelationshipType] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
一切都按我的预期工作,但是当我尝试删除关系时,流畅的 Nhibernate 会尝试将该特定记录的 CompanyId 设置为 null,而不是从中删除记录公司关系表。以下是我在 NHProf 中可以看到的查询:
UPDATE CompanyRelationship
SET CompanyId = null
WHERE CompanyId = 3893 /* @p0 */
AND Id = 487 /* @p1 */
要删除记录,我调用 RemoveRelatedCompany 函数,该函数从 relatedCompanies 列表中删除特定的 CompanyRelationship,然后调用 Session.Save() 和 Session.Flush() 来保存 Company 实体。
对于我在这里做错了什么有什么想法吗?
I am facing an issue while trying to delete a many-to-many relationship using Fluent Nhibernate. I have following domain classes:
public class Organisation
{
public virtual int Id {get; set;}
private IList<OrganisationRelationshiop> relatedOrganisations;
public virtual IList<OrganisationRelationship> RelatedOrganisation
{
get
{
return this.relatedOrganisations;
}
protected set
{
this.relatedOrganisations = value;
}
}
public virtual void RemoveRelatedOrganisation(OrganisationRelationship organisationRelationship)
{
this.relatedOrganisations.Remove(organisationRelationship);
}
}
Here is my OrganisationRelationship class that is representing the many to many relationship between orgnaisations.
public class OrganisationRelationship
{
public virtual int Id {get; set;}
public virtual Organisation Organisation{ get; set; }
public virtual OrganisationRelationshipType OrganisationRelationshipType { get; set; }
public virtual Organisation RelatedOrganisation { get; set; }
}
Her is the script for tables:
Organisation Table:
CREATE TABLE [dbo].[Organisation](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrganisationName] [nvarchar](200) NOT NULL,
CONSTRAINT [PK_Organisation] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
OrganisationRelationshop Table:
CREATE TABLE [dbo].[OrganisationRelationship](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OrganisationId] [int] NOT NULL,
[RelatedOrganisationId] [int] NOT NULL,
[OrganisationRelationshipTypeId] [int] NOT NULL,
CONSTRAINT [PK_OrganisationRelationship] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
OrganisationRelationType Table:
CREATE TABLE [dbo].[OrganisationRelationshipType](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_OrganisationRelationshipType] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Everything is working as I expect but when I try to delete a relationship, fluent Nhibernate tries to set the CompanyId to null for that particular record instead of deleting the record from the CompanyRelationship table. Here is the query that I can see in NHProf:
UPDATE CompanyRelationship
SET CompanyId = null
WHERE CompanyId = 3893 /* @p0 */
AND Id = 487 /* @p1 */
To delete the record I am calling RemoveRelatedCompany function which removes the particular CompanyRelationship from the relatedCompanies list and then I am calling Session.Save() and Session.Flush() to save the Company entity.
Any ideas as to what I am doing wrong here due to which this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我使用 AutoMapping 时,我必须按如下方式覆盖组织的映射才能解决问题:
mapping.HasMany(c => c.RelatedOrganizes).Inverse().ForeignKeyCascadeOnDelete().Table("OrganizationRelationship");
As I am using AutoMapping I had to overrid the mappings for the Organisation as follows to resolve the issue:
mapping.HasMany(c => c.RelatedOrganisations).Inverse().ForeignKeyCascadeOnDelete().Table("OrganisationRelationship");
也许你必须设置 .Cascade.AllDeleteOrphan(); (cascade=“all-delete-orphan”) 在你的映射上。
另外,如果您发布您的映射将更容易回答您
Probably you must set .Cascade.AllDeleteOrphan(); (cascade=“all-delete-orphan”) on your mappings.
Also if you post your mappings will be easier to answer you