0

Apologies if this is a dumb fool question, but I'm tired today and google isn't helping. This must be a common problem, but nothing I've found answers the question.

Whenever I've made use of Entity Framework in clickonce desktop applications, I've suffered a bit with the "entity object cannot be referenced by multiple instances of IEntityChangeTracker" error. This arises when multiple versions of the same database object are updated by different versions of the context.

On a web project, it's easy to bypass this issue by sending all the Db calls through some sort of singleton pattern on the server. If you're farming out a lot of Db update/insert calls to a service, you can do the same.

But what if you had a distributed application (e.g. WPF via clickonce) which potentially had a lot of users connecting to the database. Each user has their own install of the code, so even if you separate out the business logic from the Db logic, each user will still have their own copy of the context.

Is there any sort of pattern or technique that would allow inserts/updates direct from the application while avoiding the "multiple contexts" problem? Or is using something like a Windows service the only way to achieve this?

Bob Tway
  • 9,301
  • 17
  • 80
  • 162
  • Is it concurrency issue? can you add a timestamp in your model? – Larry Mar 23 '15 at 11:46
  • @Larry concurrency is an issue - this is a putative system which will have a fairly large number of users, some of whom will be wanting to work on the same data simultaneously. Because we're still at the planning stage, we can consider adding timestamps to records if it's going to help (and I can see how it might). I'd still be interested in a more "technical" solution, out of curiosity. – Bob Tway Mar 23 '15 at 11:56
  • well, you can still adopt the same architecture as web applications. Rather than having all your clients talk to db straightaway, You can have a middle man sits in between client and db layer. This middle man can be a webservice (for example). This way all the clients will talk to the webservice, and this webservice is the single point to connect the database and your problem is solved. – Larry Mar 23 '15 at 12:02
  • @Larry the point is that there's no need for an extra layer in a web deployment, because all users are executing the same codebase on the server. So it's easy to push them all through an injected singleton. – Bob Tway Mar 23 '15 at 12:09

1 Answers1

3

Can you reproduce this error even with a single user running your app?

Because getting "entity object cannot be referenced by multiple instances of IEntityChangeTracker" error means that you are creating 2 DBContext in your code and trying to manage a entity (created or readed) from a context through the other context. Nothing about distributed concurrency. Nothing about multiple users.

Check this SO question and try to identfy the error pattern in your code. In the example the two context are in memory but, as every entity keeps a reference to its IEntityChangeTrackeer, you can get this error even if its DbContext was disposed and you try to manage the entity with other context.

Community
  • 1
  • 1
jlvaquero
  • 8,571
  • 1
  • 29
  • 45