locked
No database provider has been configured for this DbContext error message in my console application RRS feed

  • Question

  • User-1355965324 posted

    The database connection is not being  worked in my my application , Please help

    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<TContext> object in its constructor and passes it to the base constructor for DbContext.Source=Microsoft.EntityFrameworkCore. The error message is attached with link

    https://drive.google.com/open?id=1Z6ZnE6M7EGKHjCOleQupArKzPdhYeF8q

    My code

    namespace Email.Repository
    {
        public class EmailContext : DbContext
        {
            public EmailContext(DbContextOptions<EmailContext> options)
               : base(options)
            {
            }
            public DbSet<EmailSetting> dbEmail { get; set; }
            public DbSet<Vehicle> dbVehicle { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
               // modelBuilder.Entity<EmailSetting>().ToTable("dbEmail");
                modelBuilder.Entity<Vehicle>().ToTable("dbVehicle");
            }
        }
    }
    
    
    Appsetting.cs
    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "ConnectionStrings": {
        "DefaultConnection": "Data Source=MyDesktop\\SQLEXPRESS;Initial Catalog=MyDatabase; user id=sa; password=mypwd;"
      }
    }
    
    public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
            public IConfiguration Configuration { get; }
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<EmailContext>(options => options.UseSqlServer(Configuration.GetSection("ConnectionSetting").GetValue<string>("ConnectionString")));//Add Gocontext to application
                services.AddScoped<IEmailSender, EmailSender>();
            }
           
        }
    
     
    
     public interface IVehicleRepo : IRepository<Vehicle>
        {
            ICollection<Vehicle> GetVehicleTaxdateReminder(DateTime datefrom, DateTime dateto);
        }
    
    // Repository Method GetVehicleTaxdateReminder
    
    public class VehicleRepo : Repository<Vehicle> ,IVehicleRepo
        {
            private readonly EmailContext ctx;
            
    
            public VehicleRepo(EmailContext db) : base(db)
            {
                ctx = db;
            }
    
            public ICollection<Vehicle> GetVehicleTaxdateReminder(DateTime datefrom, DateTime dateto)
            {
                ICollection<Vehicle> data = new List<Vehicle>();
                data = (from v in ctx.dbVehicle 
                        where  v.TaxDate >= datefrom
                        && v.TaxDate<= datefrom
                        select new Vehicle
                            {
                                RegistrationNo= v.RegistrationNo
                            }
                        ).ToList();
    
                return data;
            }
        }
    
    
    // How can call the method  GetVehicleTaxdateReminder  here  Please help me
      public class Program
        { 
            public static IServiceProvider Provider { get; set; }
           
            static void Main(string[] args)
            {
                var collection = new ServiceCollection();
                collection
                    .AddDbContext<EmailContext>()
                    .AddScoped<IVehicleRepo, VehicleRepo>();
                var service = collection.BuildServiceProvider();
                var _repo = service.GetService<IVehicleRepo>();
                DateTime dtfrom = DateTime.Now.AddDays(-30);
                ICollection<Vehicle> VehicleList = new List<Vehicle>();
                VehicleList = _repo.GetVehicleTaxdateReminder(dtfrom, DateTime.Now);
               
                
     
                     
            }
             
            
        }

    Monday, December 9, 2019 7:21 AM

All replies

  • User1120430333 posted

    I had the same exception message too at one point in my layered architectural styled Windows form desktop Core 3.0 solution development process. I resolved the issue by installing the EF components in the Windows form project that was referencing the DAL classlib project that has EF in it.

    When I did that, the exception went away. I tired it, becuase that's a known resolution when EF is used in a layered or n-tier styled solution,  and EF is in the persistence layer classlib project being referenced by another layer such as the Windows form project the presentation layer. 

    Monday, December 9, 2019 11:26 AM
  • User-1355965324 posted

    DA924

    I think, the connection setting from the appsetting.jason is not taking there . Thats why the message  no database provider  has been configured message is showing. I dont know how to  get that connection  setting in program main class

    "ConnectionStrings": {
        "DefaultConnection": "Data Source=MyDesktop\\SQLEXPRESS;Initial Catalog=MyDatabase; user id=sa; password=mypwd;"
      }
    Monday, December 9, 2019 2:09 PM
  • User475983607 posted

    polachan, as suggested in all your threads, read the openly published documentation.

    https://docs.microsoft.com/en-us/ef/core/

    https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli

    Monday, December 9, 2019 2:48 PM
  • User-474980206 posted

    You don’t have any code that reads the config. Normally you would pass the connect string to context when adding it to services, like in your unused startup,cs. .net has several libraries for reading config files, you should use them.

    Monday, December 9, 2019 3:18 PM
  • User1120430333 posted

    polachan

    DA924

    I think, the connection setting from the appsetting.jason is not taking there . Thats why the message  no database provider  has been configured message is showing. I dont know how to  get that connection  setting in program main class

    "ConnectionStrings": {
        "DefaultConnection": "Data Source=MyDesktop\\SQLEXPRESS;Initial Catalog=MyDatabase; user id=sa; password=mypwd;"
      }

    I don't see how the running exe can be accessing the appsettings.json file based on the code you have. In a non Core Windows desktop solution using an app.config, the app.confige is copied over to a runtime config of programname.exe.config after a successful compile of the project in Visual Stuido and copied to the location of the programname.exe file so that .NET can find the runtime.config. For the desktop Core 3.0 program, I copied the appsettings.json file to the Bin folder where the exe is located and then used System.IO to locate the exe file and read the appsettings.json file in the same location, which was an example that I gave that I got from a WPF Core 3.0 desktop solution. I don't see how the appsettins.json file located in a VS project folder is accessible for a desktop solution at runtime.

    However, the runtime exception you are dealing with has nothing to do with a missing connectionstring, and it has to do with .NET cannot figure out what the provider is. Is MS SQL Server, SQLite, Oracle, MySQL, etc. and etc? If the Repository is a classlib project with the EF model in it, then it's the two project situation as I have explained where EF has to be installed in both projects in the console application project and the repository classlib project.

    .

    Monday, December 9, 2019 4:14 PM
  • User-854763662 posted

    Hi polachan,

    As bruce said , you don't  read the config when you use AddDbContext () in the main method ,  you could configure the connectionstring by DbContext.OnConfiguring method in the DbContext directly like below:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
          optionsBuilder.UseSqlServer(@"Data Source=MyDesktop\\SQLEXPRESS;Initial Catalog=MyDatabase; user id=sa; password=mypwd;");   
        }
    }

    Best Regards ,

    Sherry

    Thursday, December 26, 2019 10:00 AM