↓ Skip to main content

EF 4.1 Code First: Many one-to-one properties for same class type on an object.

Andrew Walsh
Author
Andrew Walsh
Coder with a passion for most all things technological.

I just started using the Entity Framework recently to employ persistence for an already established code base. Initially I was using the "Model & Database First" paradigm where a designer is used to layout the data model. Although, this ended up being tedious since my POCO instance had to exactly match the POCO proxy instance in my model designer view. After upgrading to EF 4.1, I realized the "Code First" paradigm was the way to go since I had to adapt the persistence model to my already existing code.

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.
[]{.Apple-style-span style=“background-color: #f8f8f8; color: #2e2e2e; font-family: ‘Courier New’, Courier, mono; font-size: 12px;”}\

    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 ]{.Apple-style-span style=“background-color: #f8f8f8; color: #2e2e2e; font-family: monospace; font-size: 12px; white-space: pre;"}and [DeliveryAddress ]{.Apple-style-span style=“background-color: #f8f8f8; color: #2e2e2e; font-family: monospace; font-size: 12px; white-space: pre;"}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 ]{.Apple-style-span style=“background-color: #f8f8f8; color: #2e2e2e; font-family: monospace; font-size: 12px; white-space: pre;"}property from the properties' class and the context took care of the rest. So instead of two tables in the database (one for [Customer ]{.Apple-style-span style=“font-family: monospace; font-size: 12px; white-space: pre;"}and the other for [Address]{.Apple-style-span style=“font-family: monospace; font-size: 12px; white-space: pre;”}), 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
}


\

Related

Reading and writing large binary data in T-SQL and ADO.NET.

·3 mins
Introduction # In some cases, you may want to read/write binary data from/to your relational database. This can be a problem when the data set size exceeds the memory address size (eg. 32-bit processes) or the managed large object heap cannot load it all without throwing an OutOfMemoryException. In those cases, you will need to load the data into memory in chunks. Fortunately, the T-SQL syntax supports this operation.

POCO not transforming into a POCO proxy.

·1 min
A POCO (Plain Old CLR Object) must be transformed into a POCO proxy [MyNamespace.MyClass ->{System.Data.Entity.DynamicProxies.MyClass_0A943C2FC37D33304CEB497A9210C754E3F553B50315F8E6F0F7A6FAF43777F2}]{.Apple-style-span style=“font-size: x-small;”}) in order for features like lazy loading and change tracking to work. MSDN has a great article that explains how to prepare a POCO to be transformed into a proxy. If you find that an object retrieved or attached to the context does not show itself in the DynamicProxies namespace (eg. MyNamespace.MyClass instead of ...DynamicProxies.MyClass), then you probably don't have a compliant class. You may have noticed this already when hovering your mouse cursor over an instance and seeing the runtime information on it. The distilled points are that your POCO must: