简体   繁体   中英

How to call Stored Procedure from in EF 6.1?

I generated a EF 6.1 model from an existing database including a stored procedure. It seemed to create the a function with the code to execute the 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);

It seems like I could just call the function getDocumentID but that doesn't show up in the namespace. The class getDocumentID_Result does:

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

How do I proceed with calling the SP?

To add to the question, the SP was called in ADO.NET when it was a typed DataSet with the following code. It no longer returns a DataTable but the class getDocumentID_Result. How do I do the equivalent of:

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")

Actually it does return an ObjectResult which seems to be an enumerable. When I execute the line:

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

and do results. the Intellisense returns an enumerable> i guess I could check the count for the first part of matching the above VB code but how do I get the docId and documentName? Thanks!

Generally, the SP would be added as a method of the DbContext itself eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM