Entity Framework dbset not finding added entity -


i having issue understanding why when adding new entity dbset of objectcontext, entity not found looking again.

using (var db = new smartraildb())         {             var cart01 = db.carts.singleordefault(x => x.number == 0);             if (cart01 == null)             {                 cart01 = new cart { number = 0 };                 db.carts.add(cart01);              }             var cart02 = db.carts.singleordefault(x => x.number == 0); // should find cart added - right?             assert.isnotnull(cart02); // fails because cart02 not exist in db.carts collection         } 

is able tell me doing wrong here?

also late on friday here brain half asleep now.

you have update context before try access entity. do:

db.savechanges(); right after db.cart.add(cart01);


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -