使用实体框架时,为什么我的带有数据注释的列不映射 [英] Why won't my columns with Data Annotations map when using Entity Framework

查看:99
本文介绍了使用实体框架时,为什么我的带有数据注释的列不映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的课程:

I have a class that looks like this:

public class Analyst
{
    [Column("Internal_ID")]
    public int ID { get; set; } // if this is named like the column, it works
    [Column("DisplayName")]
    public string Name { get; set; }
}

该对象的填充方式如下(下一部分在一个类中)

This object is being filled like so (this next part is in a class which inherits from DbContext):

public List<T> ExecProc<T>(string proc, Dictionary<string, object> params)
{
        var parms = new List<SqlParameter>();
        var sql = new StringBuilder();
        sql.Append(sproc.StoredProcedure);
        sql.Append(" "); //  a space, that's all it is
        foreach (var key in sproc.Parameters.Keys)
        {
            sql.Append(string.Format("{0},", key));
            parms.Add(new SqlParameter(key, sproc.Parameters[key]));
        }
        if (sql[sql.Length - 1] == ',') // removing the trailing ,
        {
            sql.Remove(sql.Length - 1, 1);
        }
        return Database.SqlQuery<T>(sql.ToString(), parms.ToArray()).ToList();
}

返回的列表包含正确数量的行(在这种情况下为对象) ,但所有属性均未填充值。如果proc返回200行,那么我在列表中得到了200个对象,但是它们中没有一个填充了任何值。

The returned List contains the correct amount of rows (or objects in this case), but none of the properties are populated with values. If the proc returns 200 rows, then I get 200 objects in the List, but none of them have any values populated.

我已经验证了两个对象之间没有不匹配字段名称和[Column( name)]属性中的值。如果我更改属性名称以匹配结果集中的字段名称,那么它可以正常工作,但是如果我尝试使用数据注释映射它们,则似乎无法使其正常工作。谁能指出我在这里想念的东西吗?我确信这很简单。

I have verified that there is no mismatch between the field names and the values in the [Column("name")] attribute. If i change the property names to match the field names in the result set, then it works fine, but I cannot seem to get it to work if I try mapping them using the Data Annotations. Can anyone please point out what I'm missing here? I'm certain that it's something simple.

使用:Visual Studio 2010 Ultimate SP1,.Net 4 SP1,EF 4.2.0(我在EF 4.3中遇到了相同的问题)。所有这些都仅使用代码完成,没有映射文件用于此。

Using: Visual Studio 2010 Ultimate SP1, .Net 4 SP1, EF 4.2.0 (I encountered the same problem with EF 4.3). This is all done using code only, no mapping files are used for this.

推荐答案

看来,这已被记录为行为。 Database.SqlQuery方法没有直接连接到DbContext模型配置。

As it seems, this is documented behavior. Database.SqlQuery Method has no direct connection to DbContext model configuration.


创建原始SQL查询,该查询将返回给定通用
类型的元素。 类型可以是具有与查询返回的列的
名称相匹配的属性的任何类型
,也可以是简单的
基本类型。

Creates a raw SQL query that will return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type.

因此,使用此方法时,应构造sql,以便结果集列名与使用的类型的属性名匹配。

So, when using this method, you should construct your sql so that result set column names match property names of type used.

这篇关于使用实体框架时,为什么我的带有数据注释的列不映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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