How to call a stored procedure in EF Core 3.0 via FromSqlRaw

后端 未结 6 1135
失恋的感觉
失恋的感觉 2020-12-03 14:04

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.

相关标签:
6条回答
  • 2020-12-03 14:34

    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();
    
    0 讨论(0)
  • 2020-12-03 14:39

    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.

    0 讨论(0)
  • 2020-12-03 14:46

    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();
    
    0 讨论(0)
  • 2020-12-03 14:52

    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();
    
    0 讨论(0)
  • 2020-12-03 14:54
    var result=context.yourmodelclass.FromSqlInterpolated($"StoredProcedureName {param1},{param2}").tolist();
    

    You can add multiple parameters if needed. Note:

    • context => your name of database.
    • yourmodelclass => the class in models folder you create for fetching the output result from the stored procedure result.
    0 讨论(0)
  • 2020-12-03 14:54

    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.

    0 讨论(0)
提交回复
热议问题