简体   繁体   中英

Entity Framework stored procedure single result set

I am working with a MySQL stored procedure and trying to retrieve the single result set using EF6. My stored procedure contains the simple select statement and I have mapped it into my model. Below is the class generated by EF to map to my stored procedure

public partial class usp_aggregatedLogs_Result
{ 
}  

It's an empty class generated by EF. I have added the properties to this class to map it to the result set returned by stored procedure.

 public partial class usp_aggregatedLogs_Result
 {
    public int AccountId { get; set; }
    public string AccountName { get; set; }
    public string ProjectId { get; set; }
    public string ProjectName { get; set; }
    public string SystemId { get; set; }
    public string SystemName { get; set; }
    public string ParameterId { get; set; }
    public string ParameterName { get; set; }
    public Nullable<System.DateTime> TimeStamp { get; set; }
    public long LogId { get; set; }
    public string Type { get; set; }
}

Below is the code generated by EF in the DBContext Class

public virtual ObjectResult<usp_aggregatedLogs_Result> usp_aggregatedLogs(Nullable<System.DateTime> dateFrom, Nullable<System.DateTime> dateTo)
    {
        var dateFromParameter = dateFrom.HasValue ?
            new ObjectParameter("DateFrom", dateFrom) :
            new ObjectParameter("DateFrom", typeof(System.DateTime));

        var dateToParameter = dateTo.HasValue ?
            new ObjectParameter("DateTo", dateTo) :
            new ObjectParameter("DateTo", typeof(System.DateTime));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<usp_aggregatedLogs_Result>("usp_aggregatedLogs", dateFromParameter, dateToParameter);
    }

I assume that calling this function should return me the result set returned by stored procedure. I am calling it this way

List<usp_aggregatedLogs_Result> ResultList= obj.usp_aggregatedLogs(DateFrom, DateTo).ToList();

I receive the result in my ResultList. I receive 43 objects which is correct as my stored procedure returns 43 rows. But I get no values for the properties of these objects. all properties values are either set to 0 or null.it seems like my ResultList objects are not initialized.

I don't know how to properly call my stored procedure and retrieve its result set into my application.

Please help.

I use a bit of a trick for it.

  1. Create a view that has the same data structure as data returned by your procedure and add it to Your edmx. It doesnt have to be anything logical. Can be like:

    select 1 as AccountId, 'ABC' as AccountName , (...) from x;

    as long as it has correct data types. But if Your procedure only filters some data that You can put into a view I recommend doing a meaningfull view.

  2. When mapping Your procedure in EF choose mapped view as return type.

  3. You don't have to return the view from the procedure. You can return any data as long as it has matching structure with the view.

  4. In C# Your procedure will now return a collection of view items.

This worked for me on MSSQL so please let me know if You managed to kick it off on MySQL.

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