Unable to use existing database in unit tests with Effort framework

c# effort entity-framework entity-framework-6 unit-testing


I'm attempting to create a test using an Azure SQL-hosted database and the Entity Framework 6 Effort framework.

An exception is thrown when the following code is run:

public static void ClassInitialize(TestContext context)

public void TestMethod1()
    const string connectionString = "Data Source=***;Initial Catalog=my_catalog;User ID=user;Password=password;provider=System.Data.SqlClient";
    IDataLoader loader = new EntityDataLoader(connectionString);
    using (var ctx = new UsersDbContext(Effort.DbConnectionFactory.CreatePersistent("cool", loader)))
        var usersCount = ctx.Users.Count();

An exception was raisedCount() execution:

Effort.Exceptions.EffortException: Unhandled exception while trying to initialize the content of 'Table' table ---> System.ArgumentException: Keyword not supported: 'data source'.

When replacing, the same exception is thrown.EffortProviderConfiguration.RegisterProvider() using settings in app.config.

When creating the connection string for the database with the exact sameUsersDbContext It works, and the information is available. Additionally, context creation without a connection string using the Effort permanent or transient mode functions well.

What needs be done to set up a connection to the data already there in a genuine DB?

7/22/2014 7:02:37 PM

Popular Answer

The documentation makes it a bit clearer if you're confused like I was about why you even need to give Effort a connection string since it uses an in-memory database and you just provide your context with a connection directly. It's only necessary if you're using the database-first or model-first versions of the Entity Framework because the Entity connection string gives Effort the information it needs to find your model so that it can generate a schema So you can safely use dummy names for the server, database, user id, and password fields in the connection string.

This also clarifies that the special default DbConnectionFactory method only functions for code-first, which accounts for the initial few hours of issues I was experiencing. You must inject an Entity Connection into your entity class as specified in here in order to use model first or database first.

Creating a second code file in the same assembly, giving it the same namespace, and making it also a partial class will allow you to add the second constructor required for setting the EntityConnection to that code file instead. This will prevent the t4 template from deleting the code with the custom constructor when you modify or recreate your entity model.

11/17/2016 9:10:25 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow