我不知道为什么会这样
var blah = this.Database.SqlQuery<MyObjectWithTwoStringPropsNamedKeyAndValue>("exec mySproc").ToList();
这不是
var blah = this.Database.SqlQuery<KeyValuePair<string,string>>("exec mySproc").ToList();
“mySproc”返回三个记录,其中两个 varchar 列别名为“Key”和“Value”。
在第二行代码中,我得到了一个包含三个 KeyValuePair 的列表,但是对于列表中的每一项,两个属性(Key 和 Value)都为 null。
最佳答案
那是因为 KeyValuePair
属性,Key
和 Value
是只读属性。
Key
和Value
的值只能在构造函数中设置,以后不能更改。
SqlQuery尝试映射存储过程返回的列,但找不到写入它们的属性。文档没有说明属性必须是可写的,但很明显它不会使用参数化构造函数,而是使用属性。
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.
关于entity-framework - DbContext.Database.SqlQuery<KeyValuePair<int, string>> ("exec mySproc") 未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147281/