One of the earliest problems that I ran into was having more than one property on a class that referenced the same class type. This example was taken from a great site that explored the different EF properties.
public class Customer { public int Id { get; set; } public int Age { get; set; } public Address BillingAddress { get; set; } public Address DeliveryAddress { get; set; } }
Notice how both the BillingAddress and DeliveryAddress properties references the same class type. I kept trying to establish two 1-to-1 relationships on the class but this caused a problem with cascading deletes and foreign key constraints. I found a solution but it still required exceptional behavior on the part of my context and the database schema.
The most elegant solution was to just treat both of the properties as complex types. The only thing I had to do was remove the Id property from the properties' class and the context took care of the rest. So instead of two tables in the database (one for Customer and the other for Address), there would be only one. Below is a layout of what the table would look like.
Customer { Id Age BillingAddress_City BillingAddress_Street BillingAddress_Zipcode DeliveryAddress_City DeliveryAddress_Street DeliveryAddress_Zipcode }
No comments:
Post a Comment