Configuring DbContext Constructor

c# entity-framework-core

Question

I'm trying to use EF Core tools to manage an SqlServer database I'm designing in a C# class library. It's in a class library because I need to use the database schema in both an MVC6 website and some command line tools.

I had to convert the class library to being a netapp because the current version of the tooling doesn't support class libraries, but I don't think that's the source of my problem.

My DbContext class looks like this:

public class ConnellDbContext : IdentityDbContext<ConnellUser>
{
    public ConnellDbContext( DbContextOptions<ConnellDbContext> options )
    {
    }

    // core tables
    public DbSet<Ballot> Ballots { get; set; }
    public DbSet<Campaign> Campaigns { get; set; }
    //...
}

When I run "dotnet ef migrations list" on the Package Manager Console, I get the following error message:

No parameterless constructor was found on 'ConnellDbContext'. Either add a parameterless constructor to 'ConnellDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ConnellDbContext'.

I'm not quite sure how to resolve this. It's easy enough to insert a parameterless constructor, but when I do I get the following error:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

I >>think<< this means the console commands are not picking up the connection string information in my appsettings.json file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ConnellCampaigns;Trusted_Connection=True;MultipleActiveResultSets=true;AttachDbFilename=e:\\SqlServer\\Data\\ConnellCampaigns.mdf;"
  }
}

I'm missing something about how the EF tooling accesses the source code to do its magic. Any pointers or leads would be much appreciated.

Additional Info

Thanx to Mr. Anderson I've made a bit of progress. I added a parameterless constructor and overrode the OnConfiguring() method in my DbContext class:

protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder )
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true );

    IConfigurationRoot config = builder.Build();

    optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection") );
}

That didn't work, but explicitly including the actual connection string in the call to UseSqlServer() did. Thoughts on why the call based on "DefaultConnection" didn't work?

1
6
8/6/2016 11:32:46 PM

Popular Answer

public class ConnellDbContext : IdentityDbContext<ConnellUser>
{
    internal static string connection_string
    {
        get
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        }
    }

    public ConnellDbContext() : base(connection_string)
    {

    }
    // core tables
    public DbSet<Ballot> Ballots { get; set; }
    public DbSet<Campaign> Campaigns { get; set; }
    //...
}
0
5/17/2018 6:20:51 AM


Related Questions





Related

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