Comments (13)
Despite having the provider specified in the app.config
file, I was still getting this exception being thrown for my tests that were using Effort. I wasn't using any other provider in that assembly, so I couldn't understand why this exception was still being thrown.
tl;dr version
In one of my other tests I was creating a DbModelBuilder
which results in the DbConfiguration
being accessed and created. Once it's created, adding a handler to the Loaded
event (which Effort does) will throw the exception we are seeing here.
Long Version
One of my tests creates a DbModelBuilder
. This has the following call chain:
DbModelBuilder.ctor()
|-- DbModelBuilder.SelectConventionSet()
|-- V1ConventionSet.cctor()
|-- NotMappedTypeAttributeConvention.ctor()
|-- TypeAttributeConfigurationConvention.ctor()
|-- DbConfiguration.DependencyResolver
|-- InternalConfiguration.Instance
|-- InternalConfiguration.GetConfiguration() <-- This creates the configuration!!!
Once the configuration has been created, trying to add a handler to the DbConfiguration.Loaded
event will throw an exception. Effort tries to add a handler for that event when it is first used.
I managed to work out which test was causing the configuration to be created by adding my own handler to the DbConfiguration.Loaded
event and checking the call stack when that event is raised. The call stack should show you what test caused the event to be raised.
Since adding the event handler can throw an exception, I had to add it to the start of every test. I just created a method and called it at the start of every test.
void TrackLoaded() {
DbConfiguration.Loaded += delegate(object sender, DbConfigurationLoadedEventArgs e) {
System.Diagnostics.Debugger.Break();
};
}
Then I just debugged all my tests (Test->Debug->All Tests). Once I worked out what test was causing the Loaded
event to fire, I just added Effort.Provider.EffortProviderConfiguration.RegisterProvider()
at the start of that test, and now all of the tests run without a problem!
from entityframework-effort.
Now that xUnit.NET 2.0 is released, the solution is
-
to implement your own subclass of XunitTestFramework,
-
Do your initialization in the constructor
-
Mark the assembly with a TestFrameworkAttribute.
EG:
using Xunit.Abstractions;
using Xunit.Sdk;
[assembly: Xunit.TestFramework("MyNamespace.MyClassName", "MyAssemblyName")]
namespace MyNamespace
{
public class MyClassName : XunitTestFramework
{
public MyClassName(IMessageSink messageSink) : base(messageSink)
{
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
}
}
}
from entityframework-effort.
It happens to me when I use other provider in the same test project and a DbContext with this provider is initialized by test execution process before DbContext with Effort provider.
I have found few workarounds:
- Splitting test project into two different assemblies, one for testing with Effort, second for testing with real provider.
- Making sure that
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
is called before any test method which use DbContext with different provider (ex.[AssemblyInitializeAttribute]
).
from entityframework-effort.
I was having this issue too in NUnit and this is what I did. I created a class file in the test project.
[SetUpFixture]
public class TestSetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
}
[OneTimeTearDown]
public void RunAfterAnyTests()
{
}
}
The project will see this file and run it before it starts the tests.
from entityframework-effort.
I am having the exact same issue. vs2015, mstest
from entityframework-effort.
Thank you for reporting this. It seems the problem is that the exception is still thrown when the xml configuration is set. As a workaround there should be an app config that disables this exception.
from entityframework-effort.
Any workarounds? Still experiencing this issue...
from entityframework-effort.
@reduckted you're right, i had a small project and i did mock a context and was doing some tests on that scenario, after you pointed this out, i could fix this problem as well, thanks.
from entityframework-effort.
Is there a way to use EFFORT with an existing DataContextConfiguration? If so how.
from entityframework-effort.
@JamesIlling This probably isn't the right location for a question like this, this isn't a general purpose forum. But assuming your context class has the overload exposed which accepts a connection, the following will work
var connection = Effort.DbConnectionFactory.CreateTransient();
var context = new MyContext( connection, null );
from entityframework-effort.
I have the same issue except it only happens on the build server. The tests run perfectly on my local machine. None of the solutions I have read up on work for me. Any ideas why tests won't run on a second environment?
from entityframework-effort.
Now that xUnit.NET 2.0 is released, the solution is
to implement your own subclass of XunitTestFramework,
Do your initialization in the constructor
Mark the assembly with a TestFrameworkAttribute.
EG:
using Xunit.Abstractions;
using Xunit.Sdk;[assembly: Xunit.TestFramework("MyNamespace.MyClassName", "MyAssemblyName")]
namespace MyNamespace
{
public class MyClassName : XunitTestFramework
{
public MyClassName(IMessageSink messageSink) : base(messageSink)
{
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
}
}
}
Thanks, a solution to this at last! I was intermittently getting the same error when running unit tests in a TeamCity build, though the tests worked fine in Visual Studio.
from entityframework-effort.
@GPRogers & @dotcom9 What is the solution for NUnit?
from entityframework-effort.
Related Issues (20)
- EffortConnection.RollbackToRestorePoint incorrectly works with identity fields of entities HOT 6
- Target netstandard2.1 HOT 1
- Using multiple EDMX files within a single in-memory connection HOT 4
- In memory db ignores Unique Index HOT 1
- In memory db targeting specific migration HOT 1
- SaveChanges throws exception HOT 4
- FK violation. Value [x] does not exists in the referenced table [t] HOT 1
- How to you create a connection with IdentityDbContext ? HOT 1
- Batch Update is not working with Effort.EF6 HOT 2
- Change Tracking - Single property update of entity seems broken HOT 5
- Exception querying context during unit test HOT 3
- Nested transactions rollback should allow commit on parent transaction. HOT 2
- Z.EntityFramework.Classic.Effort v 2.2.9 is not compatible with NMemory version 3.1.2 HOT 2
- Expose IdentitySpecification so we can change the seed value HOT 3
- Object of type 'a' cannot be converted to type 'b' on Effort.Internal.Common.DatabaseReflectionHelper.InsertEntity HOT 6
- [DatabaseGenerated(DatabaseGeneratedOption.Computed)] Workaround? HOT 10
- Entity Framework Plus - InsertFromQuery command - doesn't wotk HOT 2
- ExecuteSqlCommand can't be used HOT 1
- EffortException when BulkInserting a class with a nullable enum. HOT 6
- Test runs forever without a ToList() on repository query HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework-effort.