Numerous EF relationships

Using Entity Framework 5

So, I have a Client. A Client can have many Addresses, but at least one. One of the addresses will also be set as the primary address (required). I tried various mappings, but so far I get an error message when building or when I sow the database.

Customer:

public class Customer { public int CustomerId { get; set;} public String CustomerName { get; set; } public int PrimaryAddressId { get; set; } public virtual CustomerAddress PrimaryAddress { get; set; } public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; } } 

Address:

 public class CustomerAddress : Address { public int CustomerAddressId { get; set; } public int CustomerId { get; set; } public virtual Customer Customer { get; set; } } 

This part of my display is working correctly. He is at CustomerAddress.

  this.HasRequired(c => c.Customer) .WithMany(d => d.CustomerAddresses) .HasForeignKey(c => c.CustomerId); 

But how do I specify the correct mapping to install PrimaryAddress on Customer? Or is this the wrong approach?

thanks

EDIT - using the answers of Arnolds and LueTM:

This code now works.

Customer:

 public class Customer { public int CustomerId { get; set;} public String CustomerName { get; set; } // public int PrimaryAddressId { get; set; } created in mapping public virtual CustomerAddress PrimaryAddress { get; set; } public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; } } 

Address:

 public class CustomerAddress : Address { public int CustomerAddressId { get; set; } public int CustomerId { get; set; } public virtual Customer Customer { get; set; } } 

Customer Mapping:

  modelBuilder.Entity<Customer> .HasOptional(c => c.PrimaryAddress) .WithOptionalDependent().Map(m => m.MapKey("PrimaryAddressId")); modelBuilder.Entity<Customer> .HasMany(c => c.CustomerAddresses) .WithRequired(c => c.Customer) .HasForeignKey(c => c.CustomerId) .WillCascadeOnDelete(false); 

And I use the repository to make sure that the new address is created first, saved, and then set again as primary and saved. The repository ensures that the primary is "required."

+4
source share
1 answer

Since you are not showing an exception, I must assume that you are faced with a chicken egg problem.

If you set PrimaryAddress as a required property, EF must have an existing Id address to set the foreign key (set PrimaryAddressId to Customer ). However, since Address requires a Customer , you cannot store the address in front of your customer. And if you try to save the address and the client at a time, EF will not be able to determine the correct insertion order, because it needs to insert both objects with the generated identifier of another object.

Therefore, either Address or Customer must have an optional foreign key.

I would make Customer.PrimaryAddressId optional:

 modelBuilder.Entity<Customer>().HasOptional(c => c.PrimaryAddress) .WithOptionalDependent(); 

Now you can store addresses and assign a primary address to the client in separeate transactions. But you need business logic to ensure that Customer always has a primary address.

If you want to keep cutomers and addresses in a single transaction, you could add the IsPrimary (bool) property to CustomerAddress and make sure that one address is always true .

+1
source

Source: https://habr.com/ru/post/1441069/


All Articles