0

I have a solution with the following ConfigureServices mehtod on the Startup.cs

 public void ConfigureServices(IServiceCollection services)
{
    // Add Entity Framework services to the services container.
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<RegistrationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

    // Add Identity services to the services container.
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<RegistrationDbContext>()
        .AddDefaultTokenProviders();

    // Add MVC services to the services container.
    services.AddMvc();
}

Also, this is my appsettings.json

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Data Source=<MyDataSource>;Initial Catalog=<DataBaseName>;Integrated Security=True;App=EntityFramework"
    }
  }
}

Well... in one of my controllers, I have a couple of private methods that used to run synchronously but after trying to make them asynchronous, I started to get the following Exception:

No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

These private methods are just Linq queries that do a connection to the database defined on my appsettings.json... For some extra context, here is a sample of that particular code...

public IActionResult GetInfo (string name, string job)
{
    Task<List<PersonInfo>> listOfNames = GetTotalListOfNamesAsync(name);
    Task<List<JobInfo>> listOfJobs = GetTotalListOfJobsAsync(job);

    ... some stuff here ...

    var c = listOfNames.Result;
    var d = listOfJobs.Result;
}        

private async Task<List<PersonInfo>> GetTotalListOfNamesAsync(string name)
{
    using (RegistrationDbContext context = new RegistrationDbContext())
    {
        IQueryable<PersonInfo> results;

        results = (from p in context.Persons
                   Where (p.Name.contains(name))
                   select new PersonInfo
                   {
                       Firstname = p.Name,
                       LastName = p.LastName,
                       Address = p.Address,
                       BirthDate = p.BirthDate,
                       JobName = p.JobName
                   });

        return await results.Distinct().ToAsyncEnumerable().ToList();
    }
}

private async Task<List<JobInfo>> GetTotalListOfJobsAsync(string job)
{
    using (RegistrationDbContext context = new RegistrationDbContext())
    {
        IQueryable<JobInfo> results;

        results = (from j in context.Jobs
                   Where (j.Name.contains(job))
                   select new JobInfo
                   {
                       JobName = j.JobName,
                       Address = j.Address,
                       Revenue = j.Revenue
                   });

        return await results.Distinct().ToAsyncEnumerable().ToList();
    }
}

At the time I hit this line var c = listOfNames.Result; I get the mentioned exception...

Why this is happening just when I try to make my Linq query asynchronous? Am I using wrongly the asyn-await logic? Any other thing that I am missing on my Startup.cs?

Thanks!

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
user3587624
  • 1,427
  • 5
  • 29
  • 60
  • try to define the connection string in the DbContext class. You can override the OnConfiguring method and inject the connection information. See here http://stackoverflow.com/questions/29110241/how-do-you-configure-the-dbcontext-when-creating-migrations-in-entity-framework – mariovalens Feb 11 '16 at 20:33
  • I was able to manage a solution here by adding a OnConfiguring override under the RegistrationDbContext. Thanks! – user3587624 Feb 11 '16 at 21:06

0 Answers0