c# working with Entity Framework in a multi threaded server

后端 未结 6 767
北荒
北荒 2020-11-29 00:11

What is the best practice for working with entity framework in a multi threaded server? I\'m using entity framework ObjectContext to manage all my database acti

6条回答
  •  孤城傲影
    2020-11-29 00:55

    You are treating ObjectContext like it is an extremely expensive entity, so you are instantiating once and then treating it as a "facade". There is no need to do this. If, for no other reason, the connections are pooled underneath the hood and cost very little (microsecond? - probably less?) to completely set up the "chain of objects" to use the ObjectContext abstraction.

    The ObjectContext, much like direct use of SqlConnection, etc., is designed to be used with an "instantiate as late a possible and dump as soon as possible" methodology.

    EF gives you some safety in that you have the ability to test whether you have the latest objects prior to committing (Optimistic Concurrency). This does not mean "thread safe", per se, but it accomplishes the same thing if you respect the rules.

提交回复
热议问题