Fluent-Nhibernate 多对多关系问题

发布于 2024-09-12 23:37:59 字数 2744 浏览 16 评论 0原文

我在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-09-19 23:37:59

当我使用 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");

只有一腔孤勇 2024-09-19 23:37:59

也许你必须设置 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文