locked
context.Database.ExecuteSqlCommand EFCore return -1 RRS feed

  • Question

  • User-1225169881 posted

    <g class="gr_ gr_64 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="64" data-gr-id="64"><g class="gr_ gr_68 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="68" data-gr-id="68">i've</g></g>  a stored procedure with <g class="gr_ gr_110 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="110" data-gr-id="110">not</g> output <g class="gr_ gr_434 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-del replaceWithoutSep" id="434" data-gr-id="434">parameters,</g>  when i try running it, <g class="gr_ gr_244 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="244" data-gr-id="244">i</g> get -1  as output, <g class="gr_ gr_449 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="449" data-gr-id="449"><g class="gr_ gr_444 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="444" data-gr-id="444">i've</g></g> a delete statement inside sp, sp runs individually on <g class="gr_ gr_563 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="563" data-gr-id="563">sql</g> server, but when i call from ef core it returns -1





    var parameters = new [] { new SqlParameter("@returnVal", (object)param1),
    new SqlParameter("@returnVal", (object)param2),
    new SqlParameter("@returnVal", (object)param3),
    ..........
    }; var b = await _stagingContext.Database.ExecuteSqlCommandAsync("exec proacName",parameters); //optionally i tried

    var result =  _stagingContext.SaveChanges();

    //b = -1 and c =0;

    I'm using <g class="gr_ gr_42 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="42" data-gr-id="42">EFCore</g>, 

    Saturday, February 3, 2018 12:24 AM

All replies

  • User-832373396 posted

    <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="13" data-gr-id="13">Hi</g> <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">kishore</g>,

    At first, DELETE queries, that to use the ExecuteSqlCommand, which is similar to FromSql, and returns <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="14" data-gr-id="14">int</g>, the affected rows.

    ep.

    var a =await _northWindDbContext.Database.ExecuteSqlCommandAsync("usp_CreateShipper @p0, @p1", 
            parameters: new[] { "hello", "world" });

    Sir, please change a bit with your current code.

    • change 
    var parameters = new []
    {
       new SqlParameter("@returnVal", (object)param1),
       new SqlParameter("@returnVal", (object)param2),
       new SqlParameter("@returnVal", (object)param3),
        ..........
     };   
    var b = await  _stagingContext.Database.ExecuteSqlCommandAsync("exec proacName",parameters);

    to

    var parameters = new []
    {
        (object)param1,
       (object)param2,
       (object)param3,
        ..........
     };   
    
    
    var b = await  _stagingContext.Database.ExecuteSqlCommandAsync("proacName @returnVal,@returnVal1,@returnVal2",parameters);

    Guide, example, in ef core

    https://dotnetthoughts.net/how-to-execute-storedprocedure-in-ef-core/ 

    With regards, Angelina Jolie

    Monday, February 5, 2018 10:59 AM