I recently migrated from EF Core 2.2 to EF Core 3.0.
Unfortunately, I haven\'t found a way to call a stored procedure that returns an entity.
In EF Core 2.
Solution (thanks to David Browne, you should have posted it as an answer):
Replacing Single with ToList works :-)
var createdPath = ModelContext.Paths.FromSqlRaw("AddNodeWithPathProc {0}, {1}", nodeTitle, parentPathString).ToList();
It is extremely strange… just before a few days ago I have the same problem and follow this post. I had this call:
public IEnumerable<TableChange> GetTableLastChanges(string tableName, string keyColumn, out int synchronizationVersion)
{
var parameters = new[] {
new SqlParameter("@table_name", SqlDbType.VarChar) { Direction = ParameterDirection.Input, Value = tableName },
new SqlParameter("@key_column", SqlDbType.VarChar) { Direction = ParameterDirection.Input, Value = keyColumn },
new SqlParameter("@synchronization_version", SqlDbType.BigInt) { Direction = ParameterDirection.InputOutput, Value = 0 }
};
var changes = this.TableChanges.FromSqlRaw("[dbo].[GetTableLastChanges] @table_name, @key_column, @synchronization_version OUTPUT", parameters).ToList();
synchronizationVersion = Convert.ToInt32(parameters[2].Value);
return changes;
}
Right now everything is fine and this call works as expected. Therefore I should admit that there is no problem with datasets and params return for EF on Core 3.
I am not where I can test but I think the following will work:
var createdPath = ModelContext.Paths.FromSqlRaw("AddNodeWithPathProc {0}, {1}", parm1 parm2).Single();
try to seperate SqlParameter:
SqlParameter param1 = new SqlParameter("@p0","value");
SqlParameter param2 = new SqlParameter("@p1","value");
var createdPath = ModelContext.Paths.FromSqlRaw("AddNodeWithPathProc @p0 @p1", param1,
param2).Single();
var result=context.yourmodelclass.FromSqlInterpolated($"StoredProcedureName {param1},{param2}").tolist();
You can add multiple parameters if needed. Note:
Have a look here:
https://github.com/DarioN1/SPToCore
This is a scaffolder for stored procedure, it can help you to work with sp.
Its not a third part library, it generated pure c# code to include in your project that extends current dbContext to provide stored procedure methods, parameters mapping and results classed.