如何从EF 6.1中调用存储过程?

时间:2013-12-06 14:39:09

标签: c# entity-framework stored-procedures

我从包含存储过程的现有数据库生成了EF 6.1模型。它似乎用代码创建了一个函数来执行SP:

 public virtual ObjectResult<getDocumentID_Result> getDocumentID(string appID, string @interface, string interfaceID, string sentDescription)
        {
            var appIDParameter = appID != null ?
                new ObjectParameter("AppID", appID) :
                new ObjectParameter("AppID", typeof(string));

            var interfaceParameter = @interface != null ?
                new ObjectParameter("Interface", @interface) :
                new ObjectParameter("Interface", typeof(string));

            var interfaceIDParameter = interfaceID != null ?
                new ObjectParameter("InterfaceID", interfaceID) :
                new ObjectParameter("InterfaceID", typeof(string));

            var sentDescriptionParameter = sentDescription != null ?
                new ObjectParameter("SentDescription", sentDescription) :
                new ObjectParameter("SentDescription", typeof(string));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getDocumentID_Result>("getDocumentID", appIDParameter, interfaceParameter, interfaceIDParameter, sentDescriptionParameter);

似乎我可以调用函数getDocumentID,但它不会显示在命名空间中。 getDocumentID_Result类具有:

 public partial class getDocumentID_Result
    {
        public string docID { get; set; }
        public string sentName { get; set; }
    }

如何继续拨打SP?

要添加问题,SP是在ADO.NET中调用的,当它是带有以下代码的类型化DataSet时。它不再返回DataTable,而是返回类getDocumentID_Result。我该怎么做相同的:

If getDocumentID(appid, row.interface5ID, file.ToString, "5").Rows.Count > 0 Then
                    'We need to get the docId and set it equal to the correct value
                    'Need not to populate docputresp as we are not calling the web service
                    Dim dt As SSHIP_Integrationtest.NJRREM_Gilbane.getDocumentIDDataTable
                    dt = getDocumentID(appid, row.interface5ID, file.ToString(), "5")
                    Dim documentId As String = dt.Rows(0).Item("docID")
                    Dim documentName As String = dt.Rows(0).Item("sentName")

实际上它确实返回了一个似乎是可枚举的ObjectResult。当我执行该行时:

var results = entities.getDocumentID(appId, "", file.ToString(), "5");

并做结果。 Intellisense返回一个可枚举的&gt;我想我可以检查匹配上述VB代码的第一部分的计数,但我如何获得docId和documentName?谢谢!

1 个答案:

答案 0 :(得分:4)

通常,SP将作为DbContext本身的方法添加,例如

using (var context = new MyDbContext())
{
     var results = context.getDocumentId(...);
}