Skip to content

Unable to create DB Context with SQL Server provider #120

Description

@danielgreen

Hi Andrei

Do you have (or could you prepare) any samples showing how to use (or extend) DbContextCustomization to connect to a SQL Server database, for integration test purposes please?

I have created a customization as follows:

public class TestDbContextCustomization : DbContextCustomization
{
    public override void Customize(IFixture fixture)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(@"appsettings.json", false, false)
            .AddEnvironmentVariables()
            .Build();

        var connectionString = config["ConnectionStrings:CIServerIntegrationTests"];
        Configure = o => o.UseSqlServer(connectionString);

        OmitDbSets = false;
        OnCreate = OnCreateAction.None;

        base.Customize(fixture);
    }
}

And use the customization as follows:

var fixture = new Fixture().Customize(new TestDbContextCustomization());
var context = fixture.Create<TestDbContext>();
var sale = await context.Sales.FirstOrDefaultAsync();

The db context has a normal constructor:

public TestDbContext(DbContextOptions<TestDbContext> options)
    : base(options)
    {
    }

However it errors with "No database provider has been configured for this DbContext".
Am I missing a step to properly configure the provider?
A breakpoint on the UseSqlServer method call is never hit.

Thank you

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions