2

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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).
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?
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...
0

Your Address configuration does not connect address to city.

1 Comment

I don't think I can do that in AddressConfiguration, because it inherits from ComplexTypeConfiguration<Address>... or am I wrong?
0

The class reference is intended if you want to use Entity Frameworks navigation properties. To do that you should make class references virtual. So in Address the City property should be virtual. Also for ease of setting (especially if you are using MVC) you should include the ID value on the side that holds the reference like this

public virtual City City {get; set;}
public int CityId {get; set;}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.