c# - EF 6.0 incorrect items after where clause -
c# - EF 6.0 incorrect items after where clause -
i had question ef 6.0 after fetching item db ef (firstordefault) modifying value , asking same item in collection clause.
for instance assume class foo 3 properties: id, desc, statusid foos entityset. after first fetch:
var item = foos.firstordefault(f => f.id = 5); // item values are: id:5, desc:"whatever", statusid:1 after fetching database // alter statusid item.statusid = 5 // statusitems not contain above object however... var statusitems = foos.where(f => f.statusid == 5).tolist(); // allitems contain item, statusid = 5 var allitems = foos.tolist() // or firstordefault
is behaviour expected ef? if so, can forcefulness clause run on attached objects in dbcontext without specifying .tolist() first?
a possible prepare found above is:
var item = foos.firstordefault(f => f.id = 5); // item value are: id:5, desc:"whatever", statusid: 1 after fetching database // alter statusid item.statusid = 5 // statusitems contain item var statusitems = foos.tolist().where(f => f.statusid == 5).tolist();
another way around wrap in transaction assume , calling savechanges after modifying statusid property.
i take there no way around it, apart knowing how works (and such seek filter on property did not alter create sure don't drag entire table client before filtering)?
what happens mergeoption using, appendonly
(the default value), has specific behavior. entity framework holds list of objects materialized in memory. illustrate happening:
var item = foos.firstordefault(f => f.id = 5);
entity framework has (id=5) in memory
var statusitems = foos.where(f => f.statusid == 5).tolist();
all items retrieved database have statusid = 5 inside database! not include object (id=5) because changes have not been synchronized database yet using savechanges
.
var allitems = foos.tolist();
now have list of items table. appendonly
merge alternative following:
id=1 - materialize object id=2 - materialize object ... id=5 - exists! give existing instance ...
the conclusion is: when using appendonly
alternative there 2 states, database state , entity framework cache state. if query foos
list go database, returned objects matched primary key value exists. if existing object found, instance returned.
that explains why sec situation homecoming object in question, first retrieve whole table, filter it.
without more context, hard create suggestion solving issue. likely, want save earlier.
c# entity-framework
Comments
Post a Comment