使用Fluent API进行EF代码优先1对1映射

时间:2016-04-12 16:51:28

标签: entity-framework ef-code-first ef-fluent-api

  

如果数据库架构是这样的,我将如何使用流畅的API创建映射?

public class Users 
{
    public Users() 
    {            
        PermanentAddresses = new PermanentAddresses();
        TemporaryAddresses = new TemporaryAddresses();
    }
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }   
    public virtual PermanentAddresses PermanentAddresses { get; set; }
    public virtual TemporaryAddresses TemporaryAddresses { get; set; }
} 

public class PermanentAddresses 
{
    // Primary Key and Foreign Key from Users class
    public string Id { get; set; }
    // Primary Key and Foreign Key from Addresses class
    public int AddressId { get; set; }
    public virtual Users Users { get; set; }
    public virtual Addresses Addresses { get; set; }
}

public class TemporaryAddresses 
{
    // Primary Key and Foreign Key from Users class
    public string Id { get; set; }
    // Primary Key and Foreign Key from Addresses class
    public int AddressId { get; set; }
    public virtual Users Users { get; set; }
    public virtual Addresses Addresses { get; set; }
}

public class Addresses
{
    public Addresses()
    {
        PermanentAddresses = new PermanentAddresses();
        TemporaryAddresses = new TemporaryAddresses();
        Company = new Company();
    }
    public int Id { get; set; }    
    public string CompleteAddress { get; set; } 
    public virtual PermanentAddresses PermanentAddresses { get; set; }
    public virtual TemporaryAddresses TemporaryAddresses { get; set; }
    public virtual Company Company { get; set; }
}   

我收到此错误。

无法确定类型“PermanentAddresses”和“Addresses”之间关联的主要结尾。必须使用关系流畅API或数据注释显式配置此关联的主要结尾。

2 个答案:

答案 0 :(得分:1)

你的多元化让你觉得你正在处理集合,但除此之外你可以尝试类似的东西:

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

    modelBuilder.Entity<PermanentAddresses>()
        .HasRequired(pa => pa.Users)
        .WithRequiredDependent(u => u.PermanentAddresses);

    modelBuilder.Entity<TemporaryAddresses>()
        .HasRequired(pa => pa.Users)
        .WithRequiredDependent(u => u.TemporaryAddresses);

    modelBuilder.Entity<PermanentAddresses>()
        .HasRequired(pa => pa.Addresses)
        .WithRequiredDependent(u => u.PermanentAddresses);

    modelBuilder.Entity<TemporaryAddresses>()
        .HasRequired(pa => pa.Addresses)
        .WithRequiredDependent(u => u.TemporaryAddresses);
}

https://www.safaribooksonline.com/library/view/programming-entity-framework/9781449317867/ch04s07.html

答案 1 :(得分:0)

感谢您的回复。

我添加了这个配置

modelBuilder.Entity<PermanentAddresses>().ToTable("PermanentAddresses", "user");
modelBuilder.Entity<PermanentAddresses>().HasKey(x => new { x.Id, x.AddressId });

modelBuilder.Entity<TemporaryAddresses>().ToTable("TemporaryAddresses", "user");
modelBuilder.Entity<TemporaryAddresses>().HasKey(x => new { x.Id, x.AddressId });

请检查一下 generated Tables and Columns

我想将“Addresses_Id”指向“AddressId” 和“Users_Id”到“Id”

但是,如果我尝试添加 .Map(p =&gt; p.MapKey(“Id”)); .Map(p =&gt; p.MapKey(“AddressId”)); 像这样

modelBuilder.Entity<PermanentAddresses>().HasRequired(pa => pa.Users).WithRequiredDependent(u => u.PermanentAddresses).Map(p => p.MapKey("Id"));
modelBuilder.Entity<PermanentAddresses>().HasRequired(pa => pa.Addresses).WithRequiredDependent(u => u.PermanentAddresses).Map(p => p.MapKey("AddressId"));
modelBuilder.Entity<TemporaryAddresses>().HasRequired(pa => pa.Users).WithRequiredDependent(u => u.TemporaryAddresses).Map(p => p.MapKey("Id"));
modelBuilder.Entity<TemporaryAddresses>().HasRequired(pa => pa.Addresses).WithRequiredDependent(u => u.TemporaryAddresses).Map(p => p.MapKey("AddressId"));

我收到错误。

在模型生成期间检测到一个或多个验证错误:

AddressId:Name:类型中的每个属性名称必须是唯一的。已定义属性名称“AddressId”。 Id:名称:类型中的每个属性名称必须是唯一的。已定义属性名称“Id”。 AddressId:Name:类型中的每个属性名称必须是唯一的。已定义属性名称“AddressId”。 Id:名称:类型中的每个属性名称必须是唯一的。已定义属性名称“Id”。