c# - Can .Find method return wrong results in multi-user environment -
it mentioned on msdn link following .find()
method
if entity not found in context query sent database find entity there. null returned if entity not found in context or in database. find different using query in 2 significant ways:
• round-trip database made if entity given key not found in context.
• find return entities in added state. is, find return entities have been added context have not yet been saved database.
but can cause problem? let's object marked added state, before saving database exception occurred. find might return object added state, have not been saved database later on.
second concern, if .find found object in context , object updated in database after finding it, object version on context old?
so benefits can using .find()
instead of doing search based on primary key using .where
or .firstordefault(a=>a.primarykey ==id)
?
well docs state find...
uses primary key value attempt find entity tracked context. if entity not in context query executed , evaluated against data in data source,
so searches cache first. if what's in cache old (or doesn't exist in database - , maybe never will) that's get. query if can't find entity in cache. wouldn't returns "wrong" thing because of it's defined do. if need find data might changed user or that's guaranteed exist in database you'll want stick query.
Comments
Post a Comment