0

I want to run this query:

public IList<User> GetList()
{
        using (var context = new MssqlContext())
        {
            // this throws the exception
            var query = from h in context.Users 
                        select h;

            var users = query.ToList<User>();
            return users;
        }
}

But on the marked line is this exception:

System.InvalidOperationException: 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.

The problem might be because I haven't installed all essential libraries or I have badly written my MssqlContext class.

Libraries in my project: https://i.stack.imgur.com/1UrRG.jpg

My question is: What am I missing? This should to all intents and purposes work, but is doesn't.

Similar problem was solved here: 'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync

...but it didn't help me. Thanks you for any suggestions.

Class MssqlContext:

public class MssqlContext : DbContext
{
    private readonly string _connectionString;

    public DbSet<User> Users { get; set; }

    public MssqlContext()
    {
        // Temporary solution.
        var builder = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.json", true, true);
        var config = builder.Build();
        //_connectionString = ConfigurationManager;
        _connectionString = config["ConnectionStrings:SAVConnectionString"];
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paulus141
  • 5
  • 4
  • "A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider". All I see is a private _connectionString property that's never used. S.a. https://learn.microsoft.com/en-us/ef/core/ – Christoph Lütjen May 23 '21 at 12:13
  • The provider is in the connection string. So error is saying you to not have a valid connection string. – jdweng May 23 '21 at 12:38
  • ok, thanks, I will look into it – Paulus141 May 23 '21 at 12:41
  • @jdweng My connection string looks like this: "Data Source=DESKTOP-91JG566;Initial Catalog=db_SongsAndVotes;Integrated Security=true" Am I supposed to use another form? – Paulus141 May 24 '21 at 11:23
  • The form is correct if the database (MDF file) is attached to a SQL Server. Use SQL Server Management Studio to query the database. Check the login window of SSMS. It shold say Window Credential. The server name and instance (server/instance) should be the one used for the Data Source. – jdweng May 24 '21 at 11:58
  • @jdweng I do not see Window Credential anywhere https://imgur.com/a/UfayCSK. I am using the server name as DataSource like this: "SAVConnectionString": "Data Source=L211PC19;Initial Catalog=db_SongsAndVotes;Integrated Security=true" Is this correct? – Paulus141 Jun 07 '21 at 11:02
  • The SSMS login windows has an authentication dropdown. Line 1 is : Windows Authentication. Line 2 is : SQL Server Authentication. Integrated Security uses Windows Authentication. So you should get SSMS working before trying c#. Windows Authentication requires the same user account to be on local and remote machines (Group Policy in a corporate network). I can't see image in your link. I blocked where I'm currently working and will look at the link this afternoon. – jdweng Jun 07 '21 at 11:57

0 Answers0