通过在实体框架上运行Raw SQL查询获得KeyValuePair [英] KeyValuePair from running Raw SQL query on Entity Framework

查看:67
本文介绍了通过在实体框架上运行Raw SQL查询获得KeyValuePair的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下查询和代码,我希望在KeyValuePair上返回两列.我看到返回的总行是正确的,但是所有键值对都是nul!

I have the following query and code running and I want the two columns returned on a KeyValuePair. I see the total row returned is right but all the keyvaluepairs are nul !

string query = @"Select id,name from persons";

var persons = context.Database.SqlQuery<KeyValuePair<string,string>>(query);

我已经看到 answer ,说我必须创建一个类才能获取结果;但是我的问题是我不能在KeyValuePair上得到结果吗?还是我必须定义一个属性匹配的类?

I have seen an answer saying that I have to create a class to get the result; but my question is can't I get the result on KeyValuePair ? Or I must have a class defined with properties matched ?

推荐答案

问题是

The problem is that KeyValuePair doesn't have a parameterless constructor. EF materializes an object by first creating one (by its parameterless constructor) and then setting its properties.

这篇关于通过在实体框架上运行Raw SQL查询获得KeyValuePair的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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