EF Core ExecuteSqlCommand and parameters

asp.net-core c# entity-framework entity-framework-core

Question

I have to execute an sql command on ef core 1.1.2:

 var projectParam = new SqlParameter("@projectid", SqlDbType.UniqueIdentifier).Value = inventory.ProjectId;
 var locationParam = new SqlParameter("@locationid", SqlDbType.UniqueIdentifier).Value = location.Id;
 var scanOrderParam = new SqlParameter("@scanorder", SqlDbType.Int).Value = scanOrder;
 _ctx.Database.ExecuteSqlCommand("update Inventories set ScanOrder=ScanOrder+1 where ProjectId = '@projectid' AND LocationId = '@locationid' AND ScanOrder>'@scanorder';",
                  parameters: new[] {
                                     projectParam, locationParam, scanOrderParam
                  });

It throws the exception:

  • An unhandled exception has occurred: Conversion failed when converting from a character string to uniqueidentifier.
  • If I write ScanOrder>@scanorder into the command it says that scanorder parameter must be declared. Which is an int.
  • If I don't use apostrophes at projectid and locationid it throws the exception parameter must be declared.

What is the proper way for using parameters with ef core?

Edit (solution): The problem was the way I declared parameters and give them values. If I use this form it works:

var projectParam = new SqlParameter("@projectid", SqlDbType.UniqueIdentifier);
projectParam.Value = inventory.ProjectId;
var locationParam = new SqlParameter("@locationid", SqlDbType.UniqueIdentifier);
locationParam.Value = location.Id;
var scanOrderParam = new SqlParameter("@scanorder", SqlDbType.Int);
scanOrderParam.Value = scanOrder;
1
1
7/31/2017 2:06:38 PM

Accepted Answer

 var queryItem = new SqlParameter("@table", SqlDbType.Char) {Value = yourValue};
1
11/20/2018 8:34:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow