实体框架代码第一实体拆分没有虚拟导航属性?

时间:2012-12-27 02:44:24

标签: entity-framework entity-framework-6

我整个下午一直试图解决这个问题,但是空洞了。我们的架构似乎不太正确,但我没有能力改变它。

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

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; }
}

1 个答案:

答案 0 :(得分:0)

如果你是一个野生动物园书籍在线帐户 http://my.safaribooksonline.com/book/-/9781449317867来自Julie Lerman的这本书非常实用。

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

http://msdn.microsoft.com/en-us/magazine/ee532098.aspx?sdmr=JulieLerman&sdmi=authors 请参阅“使用缺少外键进行制作”一栏

相关问题