It is frustrating if even after you have done unit testing to your program, there will be always another type of error when you try to execute it in real environment. After testing for around 2 weeks, I still deal with type of error which is probably first time to me. This morning when I created new console application in Visual C#, I encountered error stated An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
It turned out that this error related to adding new record using Entity Data Framework. I actually have known how to add new instance into EDF before around 1 year ago. It just happened that after that, I never touched this thing since my last company only used .NET 2.0 for their website.
It took me quite a while before I realized the source of the error in the code below:
The commented part is the cause of the error and after I replaced with the line below it, the error is solved immediately. Simply put, The error was happened because I obtained personnels variable using different instance of EDF entity (not CJDBEnt which is used in above function) and apparently Visual Studio would not allow me to use multiple instances to be used during insertion of new record into database.
After I change it and getting personnels from the same instance (CJDBEnt), I have no more error.