我正在尝试使用实体框架“代码优先”来映射我的实体,但在映射复杂类型时遇到问题。这是我的简化示例:
域对象看起来像:
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;}
}
和映射:
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");
}
}
抛出的异常是:“字典中不存在给定的键。”
谁能帮我?
您正在尝试将站点实体类型添加到地址复杂类型。这是不可能的。
与实体一样,复杂类型由标量属性或其他复杂类型属性组成。由于复杂类型没有键,因此除了父对象之外,实体框架无法管理复杂类型对象。
看看复杂型文章 http://msdn.microsoft.com/en-us/library/bb738472.aspx了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)