我试图找出如何制作一个桥表实体,用于多对多关系,对我的模型透明。我正在使用 EF Database First。
有问题的表...(简化)
Report
- ReportId INT PK
- ReportName VARCHAR(50)
Group
- GroupId INT PK
- GroupName VARCHAR(50)
ReportGroup
- ReportId INT PK
- GroupId INT PK
当前的类结构...(简化)
public class Report
{
public int ReportId { get; set; }
public string ReportName { get; set; }
public IList<ReportGroup> ReportGroups { get; set; }
}
public class Group
{
public int GroupId { get; set; }
public string GroupName { get; set; }
public IList<ReportGroup> ReportGroups { get; set; }
}
public class ReportGroup
{
public int ReportId { get; set; }
public Report Report { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
}
使用上述内容,要获取报告所属的组需要这样的东西......
// Getting a report's groups
var report = this.ReportService.GetReportById(123456);
var groups = report.ReportGroups.Select(x => x.Group).ToList();
这并不是我想在整个应用程序中使用的东西。理想情况下,我希望桥表和实体(ReportGroup)是透明的,允许我使用这样的实体......
// Getting a report's groups
var report = this.ReportService.GetReportById(123456);
var groups = report.Groups;
// Getting a group's reports
var group = this.ReportService.GetGroupById(1);
var reports = group.Reports;
所以我的问题是 EF Database First是否可以做到这一点,如果可以,如何使用OnModelCreating () 中的 Fluent API 正确连接。
在此先感谢您的帮助。