我有针对数据库表的 POCO 类
public class Contact
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
}
PostgreSQL 数据库中的一个函数
CREATE OR REPLACE FUNCTION test_proc_pg()
RETURNS TABLE(id integer, name character varying, Checked boolean, Mobile character varying, Email character varying) AS
$BODY$
BEGIN
RETURN QUERY select temp1.id, temp1.Name, temp1.Checked, temp1.Mobile, temp1.Email from temp1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
定义了一个通用函数来调用 PostgreSQL 函数并取回数据
public static List<T> ExecuteProc<T>(string procname, params PgSqlParameter[] param)
{
List<T> list;
string paranames = string.Empty;
foreach (PgSqlParameter p in param)
{
if (paranames != string.Empty)
paranames += ", ";
paranames = paranames + "?";
}
using (var context = new EntityContext())
{
list = context.Database.SqlQuery<T>("select " + procname + "(" + paranames + ")", param).ToList<T>();
}
return list;
}
在Controller中调用上述函数
public ActionResult ProcTest()
{
List<Contact> contacts = DAL.ExecuteProc<Contact>("test_proc_pg");
return View(contacts);
}
函数正在执行并返回List<Contact>
,但类中的所有字段都是null
.
{TestPG.Models.Contact}
Checked: false
Email: null
Id: 0
Mobile: null
Name: null
但是,如果我使用 SQL Server/SqlClient 并类似地调用 proc,它会填充所有字段。这是为 SQL Server 编写的类似函数
public static List<T> ExecuteProc<T>(string procname, params SqlParameter[] param)
{
List<T> list;
string paranames = string.Empty;
foreach (SqlParameter p in param)
{
if (paranames != string.Empty)
paranames += ", ";
paranames = paranames + "@" + p.ParameterName;
}
using (var context = new SSContext())
{
list = context.Database.SqlQuery<T>("exec " + procname + " " + paranames, param).ToList<T>();
}
return list;
}
当我从控制器调用它时,它会提供填充所有字段的类
public ActionResult ProcTest()
{
List<Contact> contacts = DAL.ExecuteProc<Contact>("test_proc_ss");
return View(contacts);
}
这是来自 SQL Server 的结果
{TestSS.Models.Contact}
Checked: false
Email: "noone@nowhere.com"
Id: 1
Mobile: "1234567890"
Name: "samtech"
我在 dotConnect PostgreSQL 上做错了什么?
我正在使用 EF6 和 dotConnect for PostgreSQL 的最新试用版。我不想导入函数。
有人可以帮忙吗?