Archives

Archives / 2011 / March
  • Associations in EF Code First: Part 2 – Complex Types

    This is the second post in a series that explains entity association mappings with EF Code First. This series includes:

    Introducing the Model

    First, let's review the model that we are going to use in order to create a Complex Type with EF Code First. It's a simple object model which consists of two classes: User and Address. Each user could have one billing address (or nothing at all–note the multiplicities on the class diagram). The Address information of a User is modeled as a separate class as you can see in the class diagram below:
    In object-modeling terms, this association is a kind of aggregation—a part-of relationship. Aggregation is a strong form of association; it has some additional semantics with regard to the lifecycle of objects. In this case, we have an even stronger form, composition, where the lifecycle of the part is fully dependent upon the lifecycle of the whole.

    Fine-grained Domain Models

    The motivation behind this design was to achieve Fine-grained domain models. In crude terms, fine-grained means more classes than tables. For example, a user may have both a billing address and a home address. In the database, you may have a single Users table with the columns BillingStreet, BillingCity, and BillingZipCode along with HomeStreet, HomeCity, and HomeZipCode. There are good reasons to use this somewhat denormalized relational model (performance, for one). In our object model, we can use the same approach, representing the two addresses as six string-valued properties of the User class. But it’s much better to model this using an Address class, where User has the BillingAddress and HomeAddress properties. This object model achieves improved cohesion and greater code reuse and is more understandable.

    Complex Types are Objects with No Identity

    When it comes to the actual C# implementation, there is no difference between this composition and other weaker styles of association but in the context of ORM, there is a big difference: A composed class is often a candidate Complex Type (aka Value Object). But C# has no concept of composition—a class or property can’t be marked as a composition. The only difference is the object identifier: a complex type has no individual identity (e.g. there is no AddressId defined on Address class) which make sense because when it comes to the database everything is going to be saved into one single table.

    Complex Type Discovery

    Code First has a concept of Complex Type Discovery that works based on a set of Conventions. The convention is that if Code First discovers a class where a primary key cannot be inferred, and no primary key is registered through Data Annotations or the fluent API, then the type will be automatically registered as a complex type. Complex type detection also requires that the type does not have properties that reference entity types (i.e. all the properties must be scalar types) and is not referenced from a collection property on another type.

    How to Implement a Complex Type with EF Code First

    The following shows the implementation of the introduced model in Code First:
    public class User
    {
        public int UserId { getset; }
        public string Name { getset; }
        
        public Address Address { getset; }
    }
     
    public class Address
    {
        public string Street { getset; }
        public string City { getset; }
        public string ZipCode { getset; }
    }
     
    public class Context : DbContext
    {
        public DbSet<User> Users { getset; }
    }
    With code first, this is all of the code we need to write to create a complex type, we do not need to configure any additional database schema mapping information through Data Annotations or the fluent API.

    Complex Types: Splitting a Table Across Multiple Types

    The mapping result for this object model is as follows (Note how Code First prefixes the complex type's column names with the name of the complex type):

    Complex Types are Required

    As a limitation of EF in general, complex types are always considered required. To see this limitation in action, let's try to add a record to the Users table:
    using (var context = new Context())
    {
        User user = new User()
        {
            Name = "Morteza"
        };
     
        context.Users.Add(user);
        context.SaveChanges();
    }
    Surprisingly, this code throws a System.Data.UpdateException at runtime with this message:
    Null value for non-nullable member. Member: 'Address'.
    If we initialize the address object, the exception would go away and the user will be successfully saved into the database:
    Now if we read back the inserted record from the database, EF will return an Address object with Null values on all of its properties (Street, City and ZipCode). This means that even when you store a complex type object with all null property values, EF still returns an initialized complex type when the owning entity (e.g. User) is retrieved from the database.

    Explicitly Register a Type as Complex

    You saw that in our model, we did not use any data annotation or fluent API code to designate the Address as a complex type, yet Code First detects it as a complex type based on Complex Type Discovery. But what if our domain model requires a new property like "Id" on Address class? This new Id property is just another scalar non-primary key property that represents let's say another piece of information about Address. Now Code First can (and will) infer a key and therefore marks Address as an entity that has its own mapping table unless we specify otherwise. This is where explicit complex type registration comes into play. There are two ways to register a type as complex:
  • Using Data Annotations
  • EF 4.1 introduces a new attribute in System.ComponentModel.DataAnnotations namespace called ComplexTypeAttribute. All we need to do is to place this attribute on our Address class:
    [ComplexType]
    public class Address
    {
        public string Id { getset; }
        public string Street { getset; }
        public string City { getset; }
        public string ZipCode { getset; }
    }
    This will keep Address as a complex type in our model despite its Id property.
  • Using Fluent API
  • Alternatively, we can use ComplexType generic method defined on DbModelBuilder class to register our Address type as complex:
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Address>();
    }

    Best Practices When Working with Complex Types

    • Always Initialize the Complex Type:
    • Because of the problem we saw, I recommended always initialize the complex type inside its owning entity's constructor.
    • Add a Read Only Property to the Complex Type for Null Value Checking:
    • Defining a non-persistent read only property like HasValue will help to test for null values.
    • Consider Always Explicitly Registering a ComplexType:
    • Even if your class is automatically detected as a complex type by Code First, I still recommend to mark it with ComplexTypeAttribute. Not only that helps your object model to be more readable but also ensures that your complex types will stay as complex as your model evolves in your project. Of course if you have a domain layer then you should use the fluent API's ComplexType method instead since coupling your POCO domain model to the EntityFramework assembly (where ComplexTypeAttribute lives) is the last thing you want to do in your layered architecture!
    Therefore, our final object model will be:
    public class User
    {
        public User()
        {
            Address = new Address();
        }
     
        public int UserId { getset; }        
        public string Name { getset; }
            
        public Address Address { getset; }
    }
     
    [ComplexType]
    public class Address
    {
        public string Street { getset; }
        public string City { getset; }
        public string ZipCode { getset; }
     
        public bool HasValue
        {
            get
            {
                return (Street != null || ZipCode != null || City != null);
            }
        }
    }
    The interesting point is that we did not have to explicitly exclude the HasValue property from the mapping above. Since HasValue has been defined as a read only property (i.e. there is no setter), EF Code First will be ignoring it based on conventions, which makes sense since a read only property is most probably representing a computed value and does not need to be persisted in the database.

    Customize Complex Type's Property Mappings at Entity Level

    We can customize the individual property mappings of the complex type. For example, The Users table now contains, among others, the columns Address_Street, Address_PostalCode, and Address_City. We can rename these with ColumnAttribute:
    public class Address
    {
        [Column("Street")]
        public string Street { getset; }
        public string City { getset; }        
        public string PostalCode { getset; }
    }
    Fluent API can give us the same result as well:
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Address>()
                    .Property(a => a.Street)
                    .HasColumnName("Street");        
    }
    Any other entity table that contains complex type fields (say, a Customer class that also has an Address) uses the same column options. Sometimes we’ll want to override the settings we made inside the complex type from outside for a particular entity. This is often the case when we try to derive an object model from a legacy database. For example, here is how we can rename the Address columns for Customer class:
    public class User
    {
        public int UserId { getset; }
        public string Name { getset; }
     
        public Address Address { getset; }
    }
        
    public class Customer
    {
        public int CustomerId { getset; }
        public string PhoneNumber { getset; }
                
        public Address Address { getset; }
    }
     
    [ComplexType]
    public class Address
    {
        [Column("Street")]
        public string Street { getset; }
        public string City { getset; }
        public string ZipCode { getset; }
    }
        
    public class Context : DbContext
    {
        public DbSet<User> Users { getset; }
        public DbSet<Customer> Customers { getset; }
     
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customer>()
                        .Property(c => c.Address.Street)
                        .HasColumnName("Customer_Street");
        }
    }

    Complex Types and the New Change Tracking API

    As part of the new DbContext API, EF 4.1 came with a new set of change tracking API that enables us to access Original and Current values of our entities. The Original Values are the values the entity had when it was queried from the database. The Current Values are the values the entity has now. This feature also fully supports complex types.

    The entry point for accessing the new change tracking API is DbContext's Entry method which returns an object of type DbEntityEntry. DbEntityEntry contains a ComplexProperty method that returns a DbComplexPropertyEntry object where we can access the original and current values:
    using (var context = new Context())
    {
        var user = context.Users.Find(1);
     
        Address originalValues = context.Entry(user)
                                        .ComplexProperty(u => u.Address)
                                        .OriginalValue;    
        
        Address currentValues = context.Entry(user)
                                       .ComplexProperty(u => u.Address)
                                       .CurrentValue;
    }
    Also we can drill down into the complex object and read or set properties of it using chained calls:
    string city = context.Entry(user)
                         .ComplexProperty(u => u.Address)
                         .Property(a => a.City)
                         .CurrentValue;
    We can even get the nested properties using a single lambda expression:
    string city = context.Entry(user)
                         .Property(u => u.Address.City)
                         .CurrentValue;

    Limitations of This Mapping 

    There are three important limitations to classes mapped as Complex Types:
  • Shared References is Not Possible:
  • The Address Complex Type doesn’t have its own database identity (primary key) and so can’t be referred to by any object other than the containing instance of User (e.g. a Shipping class that also needs to reference the same User Address, cannot do so).
  • No Elegant Way to Represent a Null Reference:
  • As we saw there is no elegant way to represent a null reference to an Address. When reading from database, EF Code First always initialize Address object even if values in all mapped columns of the complex type are null.
  • Lazy Loading of Complex Types is Not Possible:
  • Note that EF always initializes the property values of a complex type right away, when the entity instance that holds the complex object is loaded. EF does not support lazy loading for complex types (same limitation also exists if you want to have lazy loading for scalar properties of an entity). This is inconvenient when we have to deal with potentially large values (for example, a property of type byte[] on the Address complex type which has been mapped to a VARBINARY column on Users table and holds an image of the location described by the Address.).

    Summary

    In this post we learned about fine-grained domain models which complex type is just one example of it. Fine-grained is fully supported by EF Code First and is known as the most important requirement for a rich domain model. Complex type is usually the simplest way to represent one-to-one relationships and because the lifecycle is almost always dependent in such a case, it’s either an aggregation or a composition in UML. In the next posts we will revisit the same domain model and will learn about other ways to map a one-to-one association that does not have the limitations of the complex types.

    References

  • Associations in EF Code First: Part 1 – Introduction and Basic Concepts

    Earlier this month the data team shipped the Release Candidate of EF 4.1. The most exciting feature of EF 4.1 is Code First, a new development pattern for EF which provides a really elegant and powerful code-centric way to work with data as well as an alternative to the existing Database First and Model First patterns. Code First is designed based on Convention over Configuration paradigm and focused around defining your model using C#/VB.NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API.

    I’m a big fan of the EF Code First approach, and wrote several blog posts about it based on its CTP5 build: Compare to CTP5, EF 4.1 release is more about bug fixing and bringing it to a go-live quality level than anything else. Pretty much all of the API that has been introduced in CTP5 is still exactly the same (except very few changes including renaming of DbDatabase and ModelBuilder classes as well as consolidation of IsIndependent fluent API method). Therefore, the above blog posts are still usable and can (hopefully) help you in your Code First development. Having said that, I decided to complete my Code First articles by starting a whole new series instead of doing post maintenance on the current CTP5 ones.

    A Note For Those Who are New to EF and Code-First

    If you choose to learn EF you've chosen well. If you choose to learn EF with Code First you've done even better. To get started, you can find an EF 4.1 Code First walkthrough by ADO.NET team here. In this series, I assume you already setup your machine to do Code First development and also that you are familiar with Code First fundamentals and basic concepts.

    Code First And Associations

    I will start my EF 4.1 Code First articles by a series on entity association mappings. You will see that when it comes to associations, Code First brings ultimate power and flexibility. This series will come in several parts including:

    Why Starting with Association Mappings?

    From my experience with the EF user community, I know that the first thing many developers try to do when they begin using EF (specially when having a Code First approach) is a mapping of a parent/children relationship. This is usually the first time you encounter collections. It’s also the first time you have to think about the differences between entities and value types, or the type of relationships between your entities. Managing the associations between classes and the relationships between tables is at the heart of ORM. Most of the difficult problems involved in implementing an ORM solution relate to association management.

    In order to build a solid foundation for our discussion, we will start by learning about some of the core concepts around the relationship mapping and will leave the discussion for each type of entity associations to the next posts in this series.

    What is Mapping?

    Mapping is the act of determining how objects and their relationships are persisted in permanent data storage, in our case, relational databases.

    What is Relationship Mapping?

    A mapping that describes how to persist a relationship (association, aggregation, or composition) between two or more objects.

    Types of Relationships

    There are two categories of object relationships that we need to be concerned with when mapping associations. The first category is based on multiplicity and it includes three types:
    • One-to-one relationships: This is a relationship where the maximums of each of its multiplicities is one.
    • One-to-many relationships: Also known as a many-to-one relationship, this occurs when the maximum of one multiplicity is one and the other is greater than one.
    • Many-to-many relationships: This is a relationship where the maximum of both multiplicities is greater than one.
    The second category is based on directionality and it contains two types:
    • Uni-directional relationships: when an object knows about the object(s) it is related to but the other object(s) do not know of the original object. To put this in EF terminology, when a navigation property exists only on one of the association ends and not on the both.
    • Bi-directional relationships: When the objects on both end of the relationship know of each other (i.e. a navigation property defined on both ends).

    How Object Relationships are Implemented in POCO Object Models?

    When the multiplicity is one (e.g. 0..1 or 1) the relationship is implemented by defining a navigation property that reference the other object (e.g. an Address property on User class). When the multiplicity is many (e.g. 0..*, 1..*) the relationship is implemented via an ICollection of the type of other object.

    How Relational Database Relationships are Implemented?

    Relationships in relational databases are maintained through the use of Foreign Keys. A foreign key is a data attribute(s) that appears in one table and must be the primary key or other candidate key in another table. With a one-to-one relationship the foreign key needs to be implemented by one of the tables. To implement a one-to-many relationship we implement a foreign key from the “one table” to the “many table”. We could also choose to implement a one-to-many relationship via an associative table (aka Join table), effectively making it a many-to-many relationship.

    References