views:

52

answers:

2

Hi,

I'm trying to map my entities using Entity Framework "code first", but I have a problem with mapping a complex type. Here my simplified exampled:

Domain object looks like:

public class Customer
{
    public Address DeliveryAddress {get; set;}
}

public class Address
{
    public string StreetName {get; set;}
    public string StreetNumber {get; set;}
    public City City {get; set;}
}

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

and the mapping:

public class CustomerConfiguration : EntityConfiguration<Customer>
{
    public CustomerConfiguration()
    {
        this.HasKey(b => b.Id);
        this.Property(b => b.Id).IsIdentity();

        this.MapSingleType(x => new
        {
            Id = x.Id,
            DeliveryAddress_StreetName = x.DeliveryAddress.StreetName,
            DeliveryAddress_StreetNumber = x.DeliveryAddress.StreetNumber,
            DeliveryAddress_CityId = x.DeliveryAddress.City.Id, // this line causes an exception
        }).ToTable("Customer");
    }
}

public class AddressConfiguration : ComplexTypeConfiguration<Address>
{
    public AddressConfiguration()
    {           
        this.Property(b => b.StreetName).HasMaxLength(100).IsRequired().IsUnicode();
        this.Property(b => b.StreetNumber).HasMaxLength(6).IsRequired().IsUnicode();
}

public class CityConfiguration : EntityConfiguration<City>
{
    public CityConfiguration()
    {
        this.HasKey(b => b.Id);
        this.Property(b => b.Id).IsIdentity();
        this.Property(b => b.Name).IsRequired().HasMaxLength(200).IsUnicode();

        this.MapSingleType(x => new
        {
            Id = x.Id,
            Name = x.Name,
        }).ToTable("City");
    }
}

The exception that is being thrown is: 'The given key was not present in the dictionary.'

Can anyone help me?

A: 

Your Address configuration does not connect address to city.

Shiraz Bhaiji
I don't think I can do that in AddressConfiguration, because it inherits from ComplexTypeConfiguration<Address>... or am I wrong?
Ludwig Stuyck
A: 

You are trying to add Site Entity Type to Address Complex Type. This isn't possible. Like entities, complex types consist of scalar properties or other complex type properties. Because complex types do not have keys, complex type objects cannot be managed by the Entity Framework apart from the parent object.
Take a look at the Complex type article for more information.

Devart
Thanks for the answer! So in this case I should make address an aggregate (which makes not a lot of sense I guess) or I should not include City in Address, but instead the CityId (which may work for me, I don't necessarily need the City object itself).
Ludwig Stuyck
Something else: can a complex type be optional? If I assign null to Address and save it, it throws an exception that it's not nullable?
Ludwig Stuyck
http://msdn.microsoft.com/en-us/library/bb738472.aspx: Complex type properties cannot be null. An InvalidOperationException occurs when SaveChanges is called and a null complex object is encountered. I guess that answers my question. The problem is that I have an Address that is mandatory and another Address that is optional...
Ludwig Stuyck