0

我整个下午都在尝试解决这个问题,但是一无所获。我们的模式似乎不太正确,但我没有能力改变它。

基本上我需要结合这三个表:

dbo.Charity
    Id  (PK, int, not null)
    Name (varchar(100), not null)

dbo.CharityCountry
    CharityId (PK, FK, int, not null)
    CountryId (PK, FK, int, not null)

dbo.Country
    Id  (PK, int, not null)
    Name (varchar(100), not null)

并将它们放入 EF 模型中:

但我完全感到困惑,因为许多文章似乎都忽略了如何使用 Mapping 类来做到这一点:

public class CharityMap : EntityTypeConfiguration<Charity>
{
    public CharityMap()
    {
        this.HasKey(t => t.Id);

        this.ToTable("dbo.Charity");

        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Name).HasColumnName("Name");
        ...
    }
 }

我需要知道的是要放入...,我不确定如何映射主实体中没有外键的复合实体关联。

慈善对象如下所示:

public class Charity
{
    public int? Id { get; set; }
    public string Name { get; set; }
    public Country Country { get; set; }
}

和 Country 对象:

public class Country
{      
    public int? Id { get; set; }
    public string Name { get; set; }
}
4

1 回答 1

0

如果您是 safari 图书在线帐户 http://my.safaribooksonline.com/book/-/9781449317867,这本书来自 Julie Lerman,非常有用。

这是 Julie 的免费文章,关于在没有导航属性的情况下使用外键,如您的示例所示。这是可能的,但它更难使用。

http://msdn.microsoft.com/en-us/magazine/ee532098.aspx?sdmr=JulieLerman&sdmi=authors 请参阅使用缺少外键的专栏

于 2012-12-27T04:23:36.550 回答