复合主键场景
我需要一个多对多的结构,但有一个聚合约束。 我想要完成的任务可以在纯 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 在项目内是唯一的。 项目有许多选定的报告作为有序列表,每个报告都通过其项目范围分配的报告 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,我还有其他选择吗?
I need a many to many structure, but with an aggregate constraint.
What i'm trying to accomplish is done easily in pure sql, but since ActiveRecord discourages composite primary keys, i'm not sure how to accomplish what i need in recommended style.
Here is what i would have in pure 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))
This means that a project has many reports.
Each report has an assigned id, which is unique inside a project.
Project has many chosen reports as an ordered list, each of them references a report in the same project by it's project-wide assigned report id.
But here is my ActiveRecord classes, and something is missing here.
[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
}
Now, since i have surrogate keys, i don't know how to constraint ChosenReport so it can't have reference to a report from different project. So i have to enforce constraints in domain. Do i have any other options for this with ActiveRecord?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

真正的 ActiveRecord 方法是让您的类继承
ActiveRecordBase
,然后重写OnSave()
并在那里实现您的检查。但我建议在 NHibernate 拦截器 中实现检查逻辑事件监听器。The true ActiveRecord way of doing this would be making your classes inherit from
ActiveRecordBase<T>
and then overridingOnSave()
and implementing your checks there. But I recommend implementing the checking logic in a NHibernate interceptor or event listener instead.