有趣的EF4代码优先多对多关系问题

时间:2010-12-21 01:00:01

标签: c# .net-4.0 entity-framework-4 mapping ef4-code-only

我有两个模型,一个报告:

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的连接表。我尝试了许多没有成功的事情,我知道必须有一些方法可以做到,但我不知所措。希望其他人对如何做到这一点有一些了解。

0 个答案:

没有答案