entity-framework - DbContext.Database.SqlQuery<KeyValuePair<int, string>> ("exec mySproc") 未映射

标签 entity-framework entity-framework-6

我不知道为什么会这样

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 属性,KeyValue 是只读属性。

KeyValue 的值只能在构造函数中设置,以后不能更改。

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/

相关文章:

c# - WPF MVVM 中 Entity Framework 的免费数据库

c# - 无法确定关联的主要端 - Entity Framework 模型优先

c# - 使用 uint 而不是 int 作为数据模型类中的主键是个好主意吗?

c# - 是否可以从内部用常量代码包装一个方法?

asp.net-core - 应用程序中断访问 dbcontext、Asp .net 核心 web api 2.0 与 Entity Framework 核心 2.0 数据库第一种方法

c# - 将数据从 Controller 传递到 View 布局的最佳实践是什么?

asp.net-mvc - 未检查 Web API 的并发检查

c# - 不使用 saveChanges 更新 DBSet

c# - 这些 LINQ 查询之间有什么区别

c# - EntityFramework 上下文中的 SaveChanges() 静默失败