我需要一个多对多的结构,但是有一个聚合约束。我想要实现的功能在纯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在项目中是唯一的。Project有许多选定的报告作为有序列表,每个报告都通过其在项目范围内分配的报告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,我还有其他选择吗?
发布于 2010-03-17 00:53:25
真正的ActiveRecord方法是让你的类继承ActiveRecordBase<T>,然后覆盖OnSave()并在那里实现你的检查。但我建议在NHibernate interceptor或event listener中实现检查逻辑。
https://stackoverflow.com/questions/2455252
复制相似问题