4

I am attempting to use a single Address table to store addresses for several entities in the system with a generic KeyId field. A Customer can have multiple addresses and a Vendor can have multiple addresses.

Address class:

public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
// These 2 fields make it so I can get all of the addresses for a single Customer or Vendor
public string EntityType { get; set; }
public int KeyId { get; set; }

// Navigation properties
public Customer Customer { get; set; }
public Vendor Vendor { get; set; }
public Location Location { get; set; }

Customer class:

public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }

Vendor class:

public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }

DbContext:

    builder.Entity<Customer>()
        .HasMany(c => c.Addresses)
        .WithOne(a => a.Customer)
        .HasForeignKey(a => a.KeyId);

    builder.Entity<Vendor>()
        .HasMany(v => v.Addresses)
        .WithOne(a => a.Vendor)
        .HasForeignKey(a => a.KeyId);

When trying to seed the database (adding a Vendor along with a few addresses) I am running into an error that says the following:

SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Address_Customer_KeyId". The conflict occurred in database "MyDatabase", table "dbo.Customer", column 'Id'.

I am pretty sure this is because of referential integrity saying there is no customer in the database with the Id you are trying to store in KeyId.

Is there any possible way to do something like this w/ FluentAPI using EF or am I playing with fire? It just seems so crazy to have to create a class called CustomerAddress and VendorAddress if all properties are the same. It is almost as if I need to specify a dual foreign key which EF doesn't let you do.

Additional note: I think I am going to try and setup everything in SQL management studio then add a database first EF project in Visual Studio. I am curious to see how it will go about creating the model and db context.

2
  • What I think you are after is called a complex type in EF
    – Chef_Code
    Jun 1, 2017 at 5:46
  • @Chef_Code complex type is not what I would want here. It wouldn't work for one to many relationships where a customer can have multiple addresses and a vendor can have multiple addresses. Jun 1, 2017 at 11:39

1 Answer 1

1

It looks like mapping between Customer/Vendor vs Address class is not correct.

You should have different ForeignKey columns in Address table pointing to different parent tables [Customer/Vendor].

So after the changes your entities will look like:

Address:

 public int Id { get; set; }
 public string Name { get; set; }
 public string Address1 { get; set; }
 public string Address2 { get; set; }
 public string City { get; set; }
 public string State { get; set; }
 public string Country { get; set; }
 public string ZipCode { get; set; }
 public int CustomerId { get; set; }
 public int VendorId { get; set; }


 // Navigation properties
 public Customer Customer { get; set; }
 public Vendor Vendor { get; set; }

DBContext:

builder.Entity<Customer>()
    .HasMany(c => c.Addresses)
    .WithOne(a => a.Customer)
    .HasForeignKey(a => a.CustomerId);

builder.Entity<Vendor>()
    .HasMany(v => v.Addresses)
    .WithOne(a => a.Vendor)
    .HasForeignKey(a => a.VendorId);

Hope it will help.

4
  • I forgot about this option. It isn't as clean as I want. I really wanted to use KeyId. Also, I would assume in your solution CustomerId and VendorId should both be nullable? Jun 1, 2017 at 11:32
  • Yes, both CustomerId and VendorId will be nullable fields. Without having 2 separate foreign keys it will not be possible for EF to map correct Customer: Address or Vendor: Address. Lets assume a scenario where we have two records in Customer table with CustomerId 1 & 2 and two in Vendor table with VendorId 1 & 2. Now if you want to map all these records to Address table, there will be 4 records in total with duplicate 'KeyId, and that will cause problem for EF to decide which KeyId belongs to which Customer/Vendor Hope will clear your doubt. thanks Jun 2, 2017 at 4:06
  • Got it... Typically there would be an additional field in the Address table called Type which would be Customer or Vendor so when I do a select I would do address.where(a => a.Type == "Customer" && a.KeyId == 1).. But I am assuming there is no possible way to tell Entity Framework to additionally use Type as part of the foreign key. Man stuff like this makes me want to go back to using a Micro ORM like dapper and database first approach... Seems like there is so much more flexibility. Jun 2, 2017 at 14:00
  • 1
    Well.. in that case you can design SQL Composite Key from EntityFramework which works for more then one column. public class Addres { [Key, Column(Order = 0)] public int KeyId { get; set; } // Customer/Vendor Id [Key, Column(Order = 1)] public string MainTable { get; set; } //Customer/Vendor } Jun 2, 2017 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.