7

I'm trying to port a ASP.NET 4.5 application to .NET Core and I have one real issue I can't seem to figure out.

My existing application executes stored procs which return a dataset with multiple datatables. The Entity Framework can automatically map the returned fields to my entity properties but only works with the first datatable in the dataset (naturally).

So I'm just trying to figure out if its at all possible to somehow intercept the model building process and use custom code to process the dataset and look into the other datatables to set entity fields.

I know I can use the normal way of executing the stored procedure directly using the SqlConnection but I'm wondering if the Entity Framework has some way to do this already.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DKhanaf
  • 365
  • 1
  • 4
  • 18
  • Maybe this answer is helpful? http://stackoverflow.com/a/9987939/968301 – Craig Curtis Nov 04 '16 at 05:05
  • So I just noticed that DataTable and DataSet and all their friends aren't supported in .NET Core because they are considered legacy. So I have to use DbReader instead. Am I correct in this assumption? – DKhanaf Nov 04 '16 at 05:43
  • I think you can execute your stored proc this way: `dbContext.TableName.FromSql("stored_proc")` I'm not sure if that will map all of your fields from the other tables though. – Craig Curtis Nov 04 '16 at 06:30

4 Answers4

13

At the moment, the way to execute stored procedures that return data is to use the DbSet.FromSql method.

using (var context = new SampleContext())
{
    var data= context.MyEntity
        .FromSql("EXEC GetData")
        .ToList();
}

This has certain limitations:

  • It must be called on a DbSet
  • The returned data must map to all properties on the DbSet type
  • It does not support ad hoc objects.

Or you can fall back to plain ADO.NET:

using (var context = new SampleContext())
using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "GetData";
    command.CommandType = CommandType.StoredProcedure;
    context.Database.OpenConnection();
    using (var result = command.ExecuteReader())
    {
        // do something with result
    }
}

There are plans to introduce support for returning ad hoc types from SQL queries at some stage.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • That's pretty much what I concluded too from all the research I've done. One way to solve my problem is to introduce a DAO object which has properties that map to all the fields returned from the stored proc. Then use probably implicit conversion to convert the DAO into my business object applying all the necessary logic to transform fields etc... – DKhanaf Nov 04 '16 at 18:23
  • One remaining issue is the multiple datasets returned by the stored proc. Only way I can solve that is to split the storedproc into two and call them individually then follow the above pattern of DAO and in the service layer merge the to DAOs into my business object. It's probably a bit less efficient since I'm doing two calls instead of one now, but in a way a bit cleaner and more readable. – DKhanaf Nov 04 '16 at 18:25
  • It seems that in 2018 the feature is still missing, am i right ? – Muflix Oct 23 '18 at 11:51
  • 1
    @Muflix You can use Query Types: https://www.learnentityframeworkcore.com/query-types – Mike Brind Oct 23 '18 at 12:16
1

Plain ADO.NET Code would be as follows to consume the stored procedure. By using DataAdaptor:

            DataSet dataSet = null;

            using (var context = new SampleContext())
            using (var command = context.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = "GetData";
                command.CommandType = CommandType.StoredProcedure;
                context.Database.OpenConnection();
                using (SqlDataAdapter adapter = new SqlDataAdapter())
                {
                    adapter.SelectCommand = command;

                    dataSet = new DataSet();
                    adapter.Fill(dataSet);
                    return dataSet;
                }
            } 
ouflak
  • 2,458
  • 10
  • 44
  • 49
K J
  • 11
  • 2
0

To answer @DKhanaf's issue with multiple datasets, you can use the SqlDataAdapter to fill a DataSet object with all of your resultsets. SqlDataAdapter requires the full .NET Framework though, so you'd have to run your .NETCore project while targeting .NET 462 or something similar.

         using (var context = new SampleContext())
            using (var command = context.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = "GetData";
                command.CommandType = CommandType.StoredProcedure;
                context.Database.OpenConnection();
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    var ds = new DataSet();
                    adapter.Fill(ds);
                    return ds;
                }

            }
        }
snickler
  • 94
  • 3
0

I used StoredProcedureEFCore nuget package by https://github.com/verdie-g/StoredProcedureEFCore,EnterpriseLibrary.Data.NetCore,EFCor.SqlServer,EFCore.Tools

I tried DbFirst approach with {Repository pattern}.. i think so

startup.cs

ConfigureServices(IServiceCollection services){
    services.AddDbContext<AppDbContext>(opt => opt
                   .UseSqlServer(Configuration.GetConnectionString("SampleConnectionString")));
    services.AddScoped<ISomeDAL, SomeDAL>();

}
            
    public  class AppDbContext : DbContext{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {}
}

ISomeDAl Interface has {GetPropertiesResponse GetAllPropertiesByCity(int CityId);}

public class SomeDAL : ISomeDAL
{
     private readonly AppDbContext context;

     public SomeDAL(AppDbContext context)
         {
             this.context = context;
         }
     public  GetPropertiesResponse GetAllPropertiesByCity(int CityId)
     {
         //Create Required Objects for response 
         //wont support ref Objects through params
         context.LoadStoredProc(SQL_STATEMENT)
            .AddParam("CityID", CityId).Exec( r =>
             {
                  while (r.Read())
                  {

                       ORMapping<GenericRespStatus> orm = new  ORMapping<GenericRespStatus>();
                       orm.AssignObject(r, _Status);
                  }

                  if (r.NextResult())
                  {

                       while (r.Read())
                       {
                           Property = new Property();
                           ORMapping<Property> orm = new ORMapping<Property>();
                           orm.AssignObject(r, Property);
                           _propertyDetailsResult.Add(Property);
                       }
                  }    
           });
    return new GetPropertiesResponse{Status=_Status,PropertyDetails=_propertyDetailsResult}; 
    }
}

public class GetPropertiesResponse
{
     public GenericRespStatus Status;
     public List<Property> PropertyDetails;
     public GetPropertiesResponse()
         {
             PropertyDetails = new List<Property>();
         }
}
public class GenericRespStatus
{
     public int ResCode { get; set; }
     public string ResMsg { get; set; }
}
internal class ORMapping<T>
{
    public void AssignObject(IDataReader record, T myClass)
    {
        PropertyInfo[] propertyInfos = typeof(T).GetProperties();
        for (int i = 0; i < record.FieldCount; i++)
        {
            if (propertyInfos.Any(obj => obj.Name == record.GetName(i))) //&& record.GetValue(i) != DBNull.Value
            {
                propertyInfos.Single(obj => obj.Name == record.GetName(i)).SetValue(myClass, Convert.ChangeType(record.GetValue(i), record.GetFieldType(i)));
            }
        }
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Please read the [editing help](https://stackoverflow.com/editing-help) and learn how to format your answers and questions correctly. Also, please don't paste the same answer on multiple questions. If two questions can be answered with the same answer, you should answer one and flag the other as a duplicate. – David Buck Nov 13 '20 at 17:18