EF (entity framework) usage of “using” statement

前端 未结 2 851
[愿得一人]
[愿得一人] 2021-01-31 03:51

I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where \"using\" statement is us

2条回答
  •  野性不改
    2021-01-31 04:22

    I think you will find many suggesting this style of pattern. Not just me or Henk DBContext handling

    • Yes, Ideally Using statements for DBContext subtypes
    • Even better Unit Of Work patterns that are managed with Using, that have a context and dispose the context Just 1 of many UoW examples, this one from Tom Dykstra
    • The Unit Of Work Manager should be New each Http request
    • The context is NOT thread safe so make sure each thread has its own context.
    • Let EF cache things behind the scenes.
    • Test Context creation times. after several Http request. Do you still have a concern?
    • Expect problems if you store the context in static. any sort of concurrent access will hurt and if you are using parallel AJAX calls, assume 90+% chance of problems if using a static single context.

    For some performance tips, well worth a read

提交回复
热议问题