通过在实体框架上运行Raw SQL查询获得KeyValuePair [英] KeyValuePair from running Raw SQL query on Entity Framework
问题描述
我正在运行以下查询和代码,我希望在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屋!