This project has moved. For the latest updates, please go here.

EF6 + Effort.EntityConnectionFactory.CreateTransient + concurrent unit tests = fail?

Dec 6, 2013 at 9:03 PM
Hey -

I just upgraded my solution from EF5 to EF6.0.1 and from Effort to Effort.EF6. I have 60+ unit tests that use Effort.EntityConnectionFactory.CreateTransient method to get a DB connection and perform tests against the in-memory database that used to work just fine prior to the upgrades. Now they are throwing the following error when running all unit tests at once:

System.InvalidOperationException: The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

BUT...If I run each failing test individually, they execute and pass just fine. What has changed and what do I have to do to fix this issue???
Coordinator
Dec 7, 2013 at 4:21 PM
Hello,

Try to call the following method at the start of your test project.
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
For instance check the appropriate class of the Effort test project created for this purpose.

Could you provide me the entire stack trace? I will try to add a more helpful exception.
Dec 9, 2013 at 12:43 AM
Here's the full error message and stack trace. I'll try your suggestion next and let you know if that fixes it. Thanks.

Test method XXXXXX.Data.Sql.EntityFramework.Test.Unit.DataRepositoryFactoryTests.CreateRepository_Should_Create_DataRepository threw exception:
System.TypeInitializationException: The type initializer for 'Effort.EntityConnectionFactory' threw an exception. ---> System.InvalidOperationException: The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.AddLoadedHandler(EventHandler`1 handler)
at System.Data.Entity.DbConfiguration.add_Loaded(EventHandler`1 value)
at Effort.Provider.EffortProviderConfiguration.RegisterDbConfigurationEventHandler()
at Effort.Provider.EffortProviderConfiguration.RegisterProvider()
at Effort.EntityConnectionFactory..cctor()
--- End of inner exception stack trace ---
at Effort.EntityConnectionFactory.CreateTransient(String entityConnectionString)
at XXXXXX.Data.Sql.EntityFramework.Test.ContextInMemFactory.Create(String nameOrConnection, Boolean isDisposable) in ContextInMem.cs: line 39
at XXXXXX.Data.Sql.EntityFramework.Test.ContextInMemFactory.Create() in ContextInMem.cs: line 45
at XXXXXX.Data.Sql.EntityFramework.Test.Unit.DataRepositoryFactoryTests.CreateRepository_Should_Create_DataRepository() in DataRepositoryFactoryTests.cs: line 39
Dec 9, 2013 at 12:55 AM
I attempted to add the TestInitialization class and Initialize method you provided to my test project, but Effort.Test.Internal... is not recognized. Am I missing something else here?
Coordinator
Dec 9, 2013 at 8:16 AM
Edited Dec 9, 2013 at 8:16 AM
You only have to add the first line:
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
You cannot add the second line, since it is for my internal test project, so you should remove this:
Effort.Test.Internal.DataReaderInspector.DataReaderInspectorProviderConfiguration.RegisterProvider();