English 中文(简体)
使用《实体框架》时,为何不让我的专栏加上数据说明地图
原标题:Why won t my columns with Data Annotations map when using Entity Framework

我有一个类 看起来像这个:

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; }
}

此对象正在像这样被填充( 下一部分属于从 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 个对象, 但是没有一个对象包含值 。

我验证了字段名称与 [Column (“ name ”) 属性中的值之间没有不匹配之处。 如果我更改属性名称以匹配结果集中的字段名称, 那么效果会很好, 但如果我尝试用数据说明来绘制它们, 我似乎无法让它工作 。 请指出我错过了什么吗? 我确定它很简单 。

使用: Vision Studio 2010 United States SP1,.Net 4 SP1, EF 4.2.0(我遇到同样的问题,使用EF 4.3),所有这一切都只使用代码,没有为此使用绘图文件。

最佳回答

这似乎是有案可查的行为。 Database.SqlQuery方法 与DbContext模型配置没有直接联系。

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, 这样结果集的列名与所使用的类型属性名称匹配 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签