Ryan Paul Cariaga
Ryan Paul Cariaga

Reputation: 13

EF Code First 1 to 1 mapping using Fluent API

How am I going to create the mapping using fluent API if the database architecture is something like this?

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

I am getting this error.

Unable to determine the principal end of an association between the types 'PermanentAddresses' and 'Addresses'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Upvotes: 1

Views: 222

Answers (2)

Ryan Paul Cariaga
Ryan Paul Cariaga

Reputation: 13

Thank you for your reply.

I added this config

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

Kindly check this generated Tables and Columns

I want to point the "Addresses_Id" to "AddressId" and the "Users_Id" to "Id"

However, if I try to add .Map(p => p.MapKey("Id")); .Map(p => p.MapKey("AddressId")); like this

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"));

I get an error.

One or more validation errors were detected during model generation:

AddressId: Name: Each property name in a type must be unique. Property name 'AddressId' is already defined. Id: Name: Each property name in a type must be unique. Property name 'Id' is already defined. AddressId: Name: Each property name in a type must be unique. Property name 'AddressId' is already defined. Id: Name: Each property name in a type must be unique. Property name 'Id' is already defined.

Upvotes: 0

Steve Greene
Steve Greene

Reputation: 12324

Your pluralization makes it seem like you are dealing with collections, but that aside you can try something like:

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

Upvotes: 1

Related Questions