efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Support passing DbParameter instances to raw SQL APIs

Open Vasim-DigitalNexus opened this issue 11 years ago • 27 comments

Example:-

Instead of: var UserType = dbcontext.Set().FromSql("dbo.SomeSproc @p0, @p1", 45, "Ada")

Use named parameters:

object[] sqlParams = { new SqlParameter("@Id", 45), new SqlParameter("@Name", "Ada") };

var UserType = dbcontext.Set().FromSql("dbo.SomeSproc", sqlParams)

If SqlParameter is not supported then something like this would suffice:

var parameterDictionary = new Dictionary { {"@Id", 45}, {"@Name", "Ada"} };

This would make it easier to find parameters in SQL profiler, also by using a list of named parameters there would be no need to worry about parameter order

Vasim-DigitalNexus avatar Sep 12 '15 18:09 Vasim-DigitalNexus

Note for triage: We should support passing in DbParameters as well as just the raw values (we supported this in EF6), but it doesn't work at the moment.

rowanmiller avatar Sep 18 '15 17:09 rowanmiller

Dictionaries are cool and all but the 'micro-orm' approach of allowing anonymous objects to be passed in is much more convenient.

tuespetre avatar Sep 22 '15 13:09 tuespetre

I also vote for anonymous object approach to pass in the named parameters.

prasannapattam avatar Oct 02 '15 16:10 prasannapattam

Hi, is were a way to get FromSql working if I don't have Id or identity column ?

vmudinas avatar Oct 13 '15 10:10 vmudinas

Using ExecuteSQLCommand

Whats the best way of getting the value of an SqlParameter of type System.Data.ParameterDirection.Output as it stands. Do I need to go back to SqlCommand

mikes-gh avatar Dec 09 '15 09:12 mikes-gh

@mikes-gh - yes, for the moment.

rowanmiller avatar Dec 09 '15 17:12 rowanmiller

How do we return an output value ? For example I have a stored procedure that returns 0 if everything goes well and 1 if an error occurs . Based on @mikes-gh I think SqlCommand is the only way out for now or ?

tessSnap avatar Dec 09 '15 21:12 tessSnap

@tessSnap Yes, dropping down to ADO.NET is going to be the solution until we remove this limitation.

divega avatar Dec 09 '15 21:12 divega

For those of you using SQL command I found this useful to get a connection string from existing context.

someContext.Database.GetDbConnection().ConnectionString

then use a new connection .

I tried to borrow context connection and cast to SQLConnection for my command but doing that breaks the context you borrowed from.

mikes-gh avatar Dec 13 '15 10:12 mikes-gh

This code is working for me. I am using Connection object.

        SqlConnection connection = (SqlConnection)dbContext.Database.GetDbConnection();
        connection.Open();
        SqlCommand command = new SqlCommand("dbo.GetQuarrySummary @CompanyId, @StartDate, @EndDate", connection);

        command.Parameters.Add(CreateParameter(command, "@CompanyId", DbType.Int32, profile.CompanyId));
        command.Parameters.Add(CreateParameter(command, "@StartDate", DbType.DateTime, search.StartDate));
        command.Parameters.Add(CreateParameter(command, "@EndDate", DbType.DateTime, search.EndDate));

        SqlDataReader reader = await command.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection);

prasannapattam avatar Dec 14 '15 01:12 prasannapattam

Yes that works. But subtly if you try to use the context afterwards it is broken. Hence my message.

mikes-gh avatar Dec 14 '15 07:12 mikes-gh

Have you enabled Multiple Active Result Sets (MARS). https://msdn.microsoft.com/en-us/library/h32h3abf(v=vs.110).aspx

In my code, I was using the context after my SP and it is working (without enabling MARS). You need to close the connection after your initial query. Here is the code I am using

        SqlConnection connection = (SqlConnection)dbContext.Database.GetDbConnection();
        connection.Open();
        SqlCommand command = new SqlCommand("dbo.GetQuarrySummary @CompanyId, @StartDate, @EndDate", connection);

        command.Parameters.Add(CreateParameter(command, "@CompanyId", DbType.Int32, profile.CompanyId));
        command.Parameters.Add(CreateParameter(command, "@StartDate", DbType.DateTime, search.StartDate));
        command.Parameters.Add(CreateParameter(command, "@EndDate", DbType.DateTime, search.EndDate));

        SqlDataReader reader = await command.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection);

        while (await reader.ReadAsync())
        {
            //code here
        }

        reader.Close();  //this closes the connection
        YardEntity entity = await (from yd in dbContext.Yards where yd.YardId == 15 select yd).SingleAsync();

prasannapattam avatar Dec 14 '15 15:12 prasannapattam

I had to resort to SQLCommand. In EF6 I use to enjoy dbCtx.Database.SqlQuery("select... blah from...

jvelezc avatar Jan 08 '16 18:01 jvelezc

@jvelezc - you can still do the dynamic sql query as shown below

var actual = await context.Set<Customer>()
                     .FromSql(@"SELECT * FROM ""Customers"" WHERE ""ContactName"" LIKE '%z%'") 

prasannapattam avatar Jan 12 '16 02:01 prasannapattam

My problem is that Customer has to be DbSet (an entity). In the past I was able to create a viewmodel that was not part of DbSet on dbcontext class and return it.

jvelezc avatar Jan 12 '16 16:01 jvelezc

Entity Framework needs to create an object for you, hence you need to define your ViewModel as DbSet (there is no requirement of having a table with the same name in the database).

If you are looking for a dynamic object, please vote for this enhancement #2344

prasannapattam avatar Jan 12 '16 16:01 prasannapattam

So... If I do where announcement is an entity

public DbSet<Announcement> Announcement { get; set; }
   var _dbCtx = GetDbContext();
            var t = _dbCtx.Set <Announcement>()
.FromSql(@"SELECT AnnouncementId, BeginDate, EndDate, IsUrgent, Note FROM Announcement").ToArray();

Everything works as expected. However!

If I do a ViewModel and for simplicity sake I will make it exactly equal to announcement just to show that the only difference is the type.

  public DbSet<AnnouncementViewModel> AnnouncementViewModel { get; set; }

and then builder.Ignore(); //purpose of this line is not to create a table in the back end

Then

 var _dbCtx = GetDbContext();
            var t = _dbCtx.Set <Announcement>().FromSql(@"SELECT AnnouncementId, BeginDate, EndDate, IsUrgent, Note FROM Announcement").ToArray();

Creates an exception Value cannot be null.Parameter name: entityType

If I then remove builder.Ignore(); and re tun the test after having updated to the latest model changes and adding a [key] attribute to annoucement viewmodel then it works. However, it created a table in my database.

So ... how do you do it so that you can use viewmodels FromSQl('') and not have to register in the database.

jvelezc avatar Jan 12 '16 17:01 jvelezc

Are you using EnsureCreated() to create tables from code. If yes, then you need to use builder.Ignore(). Otherwise you don't need to use Ignore. You still need to declare a Key for your ViewModel, but you don't need the Table attribute.

I am using FromSql to execute a stored procedure and here is my code for this

//defining the entity class
    public class ProductSummaryEntity
    {
        [Key]
        public long RowId { get; set; }
        public int ProductTypeId { get; set; }
        public string ProductTypeName { get; set; }
        public int QuarryId { get; set; }
        public string QuarryName { get; set; }
        public int MaterialCount { get; set; }
    }

My DbContext

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
    {
           // rest of DbSet objects
           public DbSet<ProductSummaryEntity> ProductSummary { get; set; }
     }
//calling the stored proc
            return await dbContext.Set<ProductSummaryEntity>().FromSql("dbo.ProductSummaryGet @CompanyId = {0}, @QuarryIds = {1}, @ProductTypeIds = {2}, @StartDate = {3}, @EndDate = {4}"
                                 , profile.CompanyId, quarryIds, productTypeIds, search.StartDate, search.EndDate
                                 ).Select(m => Mapper.Map<ProductSummaryEntity, ProductSummaryModel>(m)).ToListAsync();

prasannapattam avatar Jan 13 '16 01:01 prasannapattam

@jvelezc Note that this issue is only about the ability to pass database provider DbParameters to raw SQL queries.

We are using a separate issue in the backlog (https://github.com/aspnet/EntityFramework/issues/1862) to track the ability to use ad-hoc queries to materialize types that are not part of the model directly.

In the meanwhile the approach explained by @prasannapattam can be used as a workaround, but it implies that there is an entity type from which you will later project your view model or DTO, and from the perspective of EF such entity has to map to a table so EnsureCreated() and generated migrations will always try to create the table for it. Having a type in the model that is not mapped to an actual table is covered in other issues.

divega avatar Jan 13 '16 19:01 divega

Thanks for this :clap: Any chance of a quick usage sample using in and out params. Maybe from your test code.

mikes-gh avatar Mar 11 '16 20:03 mikes-gh