【问题标题】:LinQ ExecuteStoreQuery calling stored procedure returning null valuesLinQ ExecuteStoreQuery 调用存储过程返回空值
【发布时间】:2012-04-25 17:55:27
【问题描述】:

我正在尝试使用 linq 调用存储过程。这是我的代码:

List<AllRowsMyDTO> list = Context.ExecuteStoreQuery<AllRowsMyDTO>("exec GetAllRows {0}", ClassID).AsQueryable().ToList();

public class AllRowsMyDTO
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public string StudentLevel { get; set; }
    public string FilePath { get; set; }
    public string StudentPreRequisites { get; set; }
    public bool IsGraded { get; set; }
}

GetAllRows 存储过程接受一个整数参数并运行一个选择语句来获取与该 ClassID 匹配的所有记录。现在该列表包含的 AllRowsMyDTO 类型的对象与存储过程返回的行数一样多。但是除了布尔属性之外,所有属性都是空的。知道这里发生了什么吗?

另外,这是使用 LinQ 调用存储过程的最佳方式吗?

感谢您的宝贵时间。

【问题讨论】:

  • 列名是否与属性名完全匹配?

标签: .net linq entity-framework stored-procedures


【解决方案1】:

问题是您没有在映射中指定键,EF 无法弄清楚它应该是什么。 EF 要求结果的某种形式的键(如果需要,可以是复合键)。

【讨论】:

  • 映射时如何指定key?
  • 一种方法是使用具有定义键的实体类型。列上的[Key] 属性可能有效;我没有测试过。
  • 嗯,我可以在 POCO 中捕获存储过程的结果,而无需任何数据注释。但是数据库字段和属性名称应该完全匹配(嗯,不区分大小写)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多