Entity Framework executes both INSERT and UPDATE stored procedures when you do INSERT -


i have stored procedure, mapped in entity framework 4.1 object. call made within

using (transactionscope transaction = new transactionscope()) {             try             {                 dbentity.car.addobject(carinfo);                 dbentity.savechanges();  /* other object savings */                  transaction.complete();                  dbentity.acceptallchanges();             }             catch (exception exp)             {                 throw exp;             }                         {                 dbentity.dispose();             } } 

i see stored procedure mapping done currently. if execute stored procedure alone on ms sql server, executes correctly.

here stored procedure

alter procedure [dbo].[carinsert] @qty               int                               ,@styleid          int                               ,@tfee             money                               ,@hwaytax          money                               ,@ofees            money                               ,@ofeesdescription ntext                               ,@mup              decimal(18, 4)                               ,@bass             money                               ,@pricemsrp        money                               ,@pricespecial     money begin   set nocount on    declare @ptotal money   declare @taxfeesnet money   declare @carid int    set @taxfeesnet = isnull(@tfee, 0) + isnull(@hwaytax, 0)                     + isnull(@ofees, 0)    if( @pricespecial null )     begin         set @ptotal = @pricemsrp + @taxfeesnet     end   else     begin         set @ptotal = @pricespecial + @taxfeesnet     end    insert car               (qty                ,styleid                ,mup                ,bass                ,pricemsrp                ,pricespecial                ,tfee                ,hwaytax                ,ofees                ,ofeesdescription                ,pricetotal)   values      (@qty                ,@styleid                ,@mup                ,@bass                ,@pricemsrp                ,@pricespecial                ,@tfee                ,@hwaytax                ,@ofees                ,@ofeesdescription                ,@ptotal)      select scope_identity() carid end  

if execute on ms sql calculates pricetotal column in table 3444.00, correct.

    @qty= 5,     @styleid = 331410,     @tfee = null,     @hwaytax = null,     @ofees = null,     @ofeesdescription = null,     @mup = 4,     @bass = 10000,     @pricemsrp = 20120,     @pricespecial = 3444 

when run mvc web application, , debug & see these values passed , pricetotal comes 20120.00

i couldn't figure out why not if else calculation & use price.

does else see weird? has been daunting few days now. appreciated. thanks

update updated title better guide others

after few minutes of posting question, figured out.

there 2 bug.

  1. entity framework used update stored procedure when try insert new records. records, update stored procedure required carid [primary key]. may ef first insert , update right away, creating new records?

  2. the update sproc had bug checking null using <>. should have been not null

in anycase, bigger issue. how force ef use insert sproc , not update when want create new record?

i tried dbentitycontext.objectstatemanager.changeobjectstate(carinfo, entitystate.added); , still ef kept on calling update sproc.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -