c# - Increase or update amount on Entity Framework -


how can translate this:

insert test (id, amount) values(1, 5)  on duplicate key  update amount=amount + values(amount)   

into entity framework mysql query?

extract logic. explained in manual:

if specify on duplicate key update, , row inserted cause duplicate value in unique index or primary key, update of old row performed. example, if column declared unique , contains value 1, following 2 statements have identical effect:

insert table (a,b,c) values (1,2,3) on duplicate key update c=c+1;      // ,  update table set c=c+1 a=1; 

your query says: insert row id 1 , amount 5, if exists, increase amount of row id 1 5.

ms-sql does, far know not support such statement, you'll have work yourself:

public void insertorupdatetest(test input) {     var entity = _dbcontext.test.firstordefault(t => t.id == input.id);      // if record doesn't exist, create , add dbset<test> tests     if (entity== null)     {         entity= new test(id = input.id);         _dbcontext.tests.add(entity);     }      // increase amount. new entity equal amount,     // whereas existing entitiy gets current value updated.     entity.amount += input.amount;      _dbcontext.savechanges(); } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -