EntityFramework 4.1 Code First错误地命名了复杂类型的列名 [英] EntityFramework 4.1 Code First incorrectly names complex type column names

查看:79
本文介绍了EntityFramework 4.1 Code First错误地命名了复杂类型的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个名为Users的表,其中包含您的典型信息:Id,名称,街道,城市-类似于此处的示例:

Say I have a table called Users, which contains your typical information: Id, Name, Street, City--much like in the example here:

本文还指出:

"Code First具有基于一组约定的复杂类型发现的概念.该约定是,如果Code First发现无法推断出主键的类,并且没有通过数据注释或流式API,则该类型将被自动注册为复杂类型.复杂类型检测还要求该类型不具有引用实体类型的属性(即,所有属性必须为标量类型),并且不能从集合属性中进行引用在另一种类型上."我的地址类满足以下条件:它由字符串组成,在其他任何地方都没有使用.

"Code First has a concept of Complex Type Discovery that works based on a set of Conventions. The convention is that if Code First discovers a class where a primary key cannot be inferred, and no primary key is registered through Data Annotations or the fluent API, then the type will be automatically registered as a complex type. Complex type detection also requires that the type does not have properties that reference entity types (i.e. all the properties must be scalar types) and is not referenced from a collection property on another type." My Address class meets these criteria: it's made up of strings and isn't used anywhere else.

尽管在应用程序中(我不确定这是否有任何区别),但我们在给用户打其他名称-例如Techs.我想将用户"的地址"列分成一个地址",以便每个技术人员都可以拥有自己的地址.根据上面的文章,EF应该推断出这一点并自动处理复杂类型.但是,当上下文尝试向我提供技术时,我得到的是以下异常:

In the app, though (I'm not sure if this makes any difference), we're calling the Users something else--say, Techs. I want to break out User's address columns into an Address so each Tech can have its own Address. According to the article above, EF should infer this and take care of the complex type automatically. What I'm getting,though, when the context attempts to give me a Tech, is the following exception:

System.Data.EntityCommandExecutionException: An error occurred while executing t
he command definition. See the inner exception for details. ---> System.Data.Sql
Client.SqlException: Invalid column name 'Address_Street'.
Invalid column name 'Address_City'.
Invalid column name 'Address_State'.
Invalid column name 'Address_Zip'.

看起来它在试图弄清Tech.Address属性,但给每个子属性赋予了错误的名称(例如,"Address_City"而不是"City").

It looks like it's trying to make sense of the Tech.Address property, but is giving each of its sub-properties the wrong name (e.g., "Address_City" instead of "City").

关于如何纠正这一点的任何想法?

Any ideas on how I can rectify this?

推荐答案

这是正确的行为.默认约定始终以类型名称作为映射到复杂类型的属性的前缀.如果要使用其他列名,则必须通过数据注释将它们映射:

That is correct behavior. Default convention always prefixes properties mapped to complex type with type name. If you want to use different column names you must map them either through data annotations:

public class Address
{
    [Column("City")]
    public string City { get; set; }
    ...
}

或通过流畅的API:

modelBuilder.ComplexType<Address>().Property(a => a.City).HasColumnName("City");

这篇关于EntityFramework 4.1 Code First错误地命名了复杂类型的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆