问题描述
我正在尝试定义一个与表中相同的复杂类型原则类。基本上,这是很好的旧用户和地址示例。
public class Customer
{
[Key]
public int customerId {get;组; }
//某些属性
public string street {get;组; }
public string city {get;组; }
public string province {get;组; }
public string country {get;组; }
public string postal {get;组; }
}
所以我试图将地址信息分解成自己的类: / p>
public class Customer
{
[Key]
public int customerId {get;组; }
//某些属性
public Address address {get;组;
}
[ComplexType]
public class Address
{
public string street {get;组; }
public string city {get;组; }
public string province {get;组; }
public string country {get;组; }
public string postal {get;组;
}
我没有编译错误,当我加载访问客户的视图模型,我在字段设置错误中找到一个未知列。
我基本上按照这个例子:
有什么我缺少或与EF5不同的东西?
默认情况下,EF希望以{complextypename_propertyname}形式复制类型的属性列。如果您手动创建表,并以不同的方式命名列,那么将不匹配。您可以尝试相应地重命名列(i。 e。街道到address_street),并尝试,如果它的工作。或者,您应该能够向复合类型的属性添加属性,以告知EF不应使用约定,而是您指定的名称(例如街道属性的[Column(street)])。
Bear with me as I'm new to C# and programming in general.
I'm trying to define a complex type that is in the same table as the principle class. Basically, it's the good old User and Address example.
public class Customer
{
[Key]
public int customerId { get; set; }
//some attributes
public string street { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string postal { get; set; }
}
So I try to slice off the address information into its own class:
public class Customer
{
[Key]
public int customerId { get; set; }
//some attributes
public Address address { get; set; }
}
[ComplexType]
public class Address
{
public string street { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string postal { get; set; }
}
I get no compile error and when I load a view that access the Customer model, I get an unknown column in field set error.
I basically followed this example: http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx
Is there something I"m missing or something different with EF5?
By default EF expects columns for properties of complex types in form {complextypename_propertyname}. If you created your tables manually and named columns differently there will be a mismatch. Can you try renaming the columns accordingly (i.e. street to address_street) and try if it works. Alternatively you should be able to add an attribute to the properties on the complex type to tell EF that is should not use the convention but the name you specified (e.g. [Column("street")] for the street property).
这篇关于实体框架5复杂类型和未知列字段列表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!