Entity framework 实体框架一对一关系映射在代码中展开

Entity framework 实体框架一对一关系映射在代码中展开,entity-framework,entity-framework-4.1,Entity Framework,Entity Framework 4.1,我有一个这样的桌子结构 Address: AddressId int not null primary key identity ...more columns AddressContinental: AddressId int not null primary key identity foreign key to pk of Address County State AddressInternational: AddressId int not null primary key iden

我有一个这样的桌子结构

Address:
AddressId int not null primary key identity
...more columns

AddressContinental:
AddressId int not null primary key identity foreign key to pk of Address
County
State

AddressInternational:
AddressId int not null primary key identity foreign key to pk of Address
ProvinceRegion
我无法控制模式,这就是它的方式

现在,我想做的是有一个地址对象

public class Address
{
    public int AddressId { get; set; }
    public County County { get; set; }
    public State State { get; set }
    public ProvinceRegion { get; set; }
}
我想让EF将其作为单个实体从数据库中拉出。保存时,我想保存单个实体,并让EF知道如何将其拆分为三个表

我如何首先在EF4.1代码中映射这一点

我一直在四处寻找,还没有找到任何符合我情况的东西

更新


一个地址记录将在
地址
中有一个记录,在
地址大陆
地址国际
中有一个记录,但不能两者都有。

下面是我将使用的流畅API

mb.Entity<Address>()
  .Map(m =>
  {
    m.Properties(p => new
    {
      p.AddressId
    });
    m.ToTable("Address");
  });

mb.Entity<Address>()
  .Map(m =>
  {
    m.Properties(p => new
    {
      p.AddressId,
      p.County,
      p.State
    });
    m.ToTable("AddressContinental");
  });

mb.Entity<Address>()
  .Map(m =>
  {
     m.Properties(p => new
     {
       p.AddressId,
       p.ProvinceRegion
     });
     m.ToTable("AddressInternational");
  });
mb.Entity()
.Map(m=>
{
m、 属性(p=>new
{
p、 地址ID
});
m、 ToTable(“地址”);
});
mb.实体()
.Map(m=>
{
m、 属性(p=>new
{
p、 地址ID,
p、 县,
p、 陈述
});
m、 ToTable(“大陆”);
});
mb.实体()
.Map(m=>
{
m、 属性(p=>new
{
p、 地址ID,
p、 省域
});
m、 ToTable(“地址国际”);
});
地址记录将有一个记录 地址是,两个都是 地址:大陆 这是国际性的,但不是两者都有

这个要求使您的第一个要求无法实现@OpticalDelusion向您展示了如何映射实体拆分,但它仅在需要所有部分时才起作用。EF不能有条件地执行此操作。此外,如果您试图保存一个新的拆分实体,它将始终在所有3个表中创建一个新记录

您需要的不是展平对象,而是TPT(每种类型的表)继承,其中您将有:

public abstract class Address
{
    public int Id { get; set; }
    public string Name { get; set; }

}

public class AddressContinental : Address
{
    public string Country { get; set; }
    public string State { get; set; }
}

public class AddressInternational : Address
{
    public string ProvinceRegion { get; set; }
}

public class Context : DbContext
{
    public DbSet<Address> Addresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<AddressContinental>().ToTable("AddressContinental");
        modelBuilder.Entity<AddressInternational>().ToTable("AddressInternational");
    }
}
公共抽象类地址
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类地址:地址
{
公共字符串国家{get;set;}
公共字符串状态{get;set;}
}
公共类地址国际:地址
{
公共字符串提供程序区域{get;set;}
}
公共类上下文:DbContext
{
公共数据库集地址{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().ToTable(“AddressContinental”);
modelBuilder.Entity().ToTable(“AddressInternational”);
}
}

}

这似乎只有在大陆和国际上都有记录的情况下(检索时)才起作用。将有一个记录在地址和一个记录在大陆或国际,但不是两者都有。我没提那件事。我会更新这个问题。我也投票支持你的答案。感谢您解决最初提出的问题。