复合主键方案

时间:2010-03-16 14:40:16

标签: primary-key castle-activerecord

我需要多对多的结构,但有一个聚合约束。 我想要完成的是在纯sql中轻松完成,但由于ActiveRecord不鼓励复合主键,我不知道如何在推荐的样式中完成我需要的东西。

以下是我在纯sql中的内容:

table Project (ID int)

table Report (ProjectWideID nvarchar(50), ProjectID int, primary key (ProjectWideID, ProjectID))

table ChosenReport(ListOrder int, ProjectWideReportID, ReportID, primary key (ProjectID,ProjectWideReportID))

这意味着项目有很多报告。 每个报告都有一个已分配的ID,在项目中是唯一的。 项目将许多选定的报告作为有序列表,每个报告通过项目范围内分配的报告ID引用同一项目中的报告。

但这是我的ActiveRecord课程,这里缺少一些东西。

[ActiveRecord]
public class Project
{
 [PrimaryKey]
 public int ID { get; set; }
 [HasMany] IList<Report> Reports { get; set; }
 [HasMany] IList<ChosenReport> ChosenReports { get; set; }
}

[ActiveRecord]
public class Report
{
 [PrimaryKey]
 public int ID { get; set; }
 [BelongsTo("ProjectID")]
 public Project ParentProject { get; set; }

 // ... other properties
}

[ActiveRecord]
public class ChosenReport
{
 // This one must be a key property
 [BelongsTo("ParentProjectID")]
 Project ParentProject { get; set; }

 // This one must be a key property
 [BelongsTo("ParentProjectID")]
 Report ParentReport { get; set; }

 // ... other properties
}

现在,由于我有代理键,我不知道如何约束ChosenReport,因此它不能引用来自不同项目的报告。所以我必须在域中强制执行约束。我对ActiveRecord有什么其他选择吗?

1 个答案:

答案 0 :(得分:0)

真正的ActiveRecord方法是让您的类继承自ActiveRecordBase<T>,然后覆盖OnSave()并在那里实施检查。但我建议在NHibernate interceptorevent listener中实施检查逻辑。