ASP.NET MVC migration hatası

Calimero

Hectopat
Katılım
19 Aralık 2020
Mesajlar
120
Daha fazla  
Cinsiyet
Erkek
Performans ödevim için bilgisayar satış sayfası yapacağım fakat nuget package Console'a "add-migration init" yazınca bu hatayı veriyor.

Hata:
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.
 
Bu hatanın nedeni, DbContext sınıfınızın yapılandırılmasının hatalı olması sanırım.
SQL Server kullanıyorsanız, OnConfiguring metodunda UseSqlServer yöntemini kullanmalısınız:
Kod:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer("connection_string_here");
    }
}
DbContextOptions<TContext> nesnesini kabul ettiğinden emin misiniz ? Ve bunu baz yapılandırıcıya iletin.
Kod:
public class YourDbContext : DbContext
{
    public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)
    {
    }

}
ConfigureServices metodunda DbContext'i hizmetlere eklediniz mi? Eklemediyseniz bundan yararlanabilirsiniz:
Kod:
public void ConfigureServices(IServiceCollection services)
{
    // Diğer hizmetler...

    services.AddDbContext<YourDbContext>(options =>
    {
        options.UseSqlServer(Configuration.GetConnectionString("YourConnectionString"));
    });

    // Diğer hizmetler...
}
 

Technopat Haberler

Yeni konular

Geri
Yukarı