有趣的 EF4 Code First 多对多关系问题

发布于 2024-10-08 05:46:37 字数 1339 浏览 2 评论 0原文

我有两个模型,一个 Report:

public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ChargeType ChargeTypes { get; set; }
}

public class ReportConfiguration : EntityConfiguration<Report>
{
    public ReportConfiguration()
    {
        MapSingleType(r => new { r.Id, r.Name }).ToTable("Report");

        HasMany(r => r.ChargeTypes)
          .WithMany(c => c.Reports)
          .Map("Report_ChargeType", (r,c) => new { ReportId = r.Id,
                                                   ChargeTypeId = c.Id });
    }
}

和一个 ChargeType:

public class ChargeType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int SortOrder { get; set; }

    public virtual Report Reports { get; set; }
}

public class ChargeTypeConfiguration : EntityConfiguration<ChargeType>
{
    public ReportConfiguration()
    {
        MapSingleType(c => new { c.Id, c.Name }).ToTable("ChargeType");

        // map SortOrder property to join table "Report_ChargeType"
        MapSingleType(c => c.SortOrder).ToTable("Report_ChargeType"); // doesn't work

    }
}

我希望能够将 ChargeType 的 SortOrder 属性映射到 Report 和 ChargeType 的联接表。我尝试了很多事情但没有成功,我知道必须有某种方法可以做到这一点,但我不知所措。希望其他人对如何做到这一点有一些见解。

I have two models, a Report:

public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ChargeType ChargeTypes { get; set; }
}

public class ReportConfiguration : EntityConfiguration<Report>
{
    public ReportConfiguration()
    {
        MapSingleType(r => new { r.Id, r.Name }).ToTable("Report");

        HasMany(r => r.ChargeTypes)
          .WithMany(c => c.Reports)
          .Map("Report_ChargeType", (r,c) => new { ReportId = r.Id,
                                                   ChargeTypeId = c.Id });
    }
}

and a ChargeType:

public class ChargeType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int SortOrder { get; set; }

    public virtual Report Reports { get; set; }
}

public class ChargeTypeConfiguration : EntityConfiguration<ChargeType>
{
    public ReportConfiguration()
    {
        MapSingleType(c => new { c.Id, c.Name }).ToTable("ChargeType");

        // map SortOrder property to join table "Report_ChargeType"
        MapSingleType(c => c.SortOrder).ToTable("Report_ChargeType"); // doesn't work

    }
}

I want to be able to map the SortOrder property of the ChargeType to the join table of Report and ChargeType. I have tried many things with no success, and I know there has to be some way to do it but I am at a loss. Hopefully someone else has some insight on how this could be done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文