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?