2 Answers
2

Locking isn't actually the main problem you are going to have here. EF will only lock for the duration of a save changes call (its actually one of the big benefits of using a tracking graph over partially committed transactions which most other ORMs use).

What is going to cause you greif is the tracking graph itself. How EF works (in most cases) is that it keeps a copy of every entity its ever seen and loops through them to find whats changed and run a process called fixups which makes navigation properties work with backlinks. This process loops through every entity the context has ever seen and is called on a bunch of operations (add, attach, delete, save, query and a few others). This means if the tracking graph is large, this process can take quite a bit of time. If you keep your context alive forever the tracking graph size tends toward the size of your database, making it unwieldy and slow.

@davea On a bit of a sidenote I use dependency injection to manage context lifecycle, with either a new instance per request (in a web based scenario) or a new instance every time.
–
Luke McGregorJan 25 '13 at 17:10

Luke, I've read about DI as a pattern and understand some of its value. Would using a framework like Ninject help me here?
–
Dave AlperovichJan 25 '13 at 17:11

@davea I use ninject in all of my web projects (and some other types of projects too) and find it really useful. The problem that it solves is object lifecycle, ie when/how to construct objects and when to dispose them. In my opinion its most useful for the consumers of Contexts but I still use it to manage to context lifecycle as well. Personally I recommend it, If you want to see an example of how I use ninject and EF check out the code on my generic repository post: blog.staticvoid.co.nz/2011/10/13/… the example solution at the bottom is useful.
–
Luke McGregorJan 25 '13 at 17:29

Thanks Luke! I will definitely read up on it. If you have time I suggest you revise your answer to include Ninject. Thanks again.
–
Dave AlperovichJan 25 '13 at 17:37

If you're using EF on service layer - then the concurrency might be an issue as I don't think that using EF context is thread safe, ie that you can use it from all threads at the same time without problems

If you have your entities tracked by the context (and I think even if you don't), the context would in time get quite large, eventually it could contain all of your database entities and then you'll run into performance problems