Reputation: 31
I Have an 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 When I call this method:
public class BaseRepository<T> where T : class
{
private readonly TourDBContext context;
public BaseRepository()
{
this.context = new TourDBContext();
}
public async Task<List<T>> GetAllAsync()
{
try
{
var list = await context.Set<T>().ToListAsync();
return list;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
public class CategoryRepository : BaseRepository<Category>
{
}
Here is my Program.cs:
builder.Services.AddDbContext<TourDBContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("TourDBConnectionString")));
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<TourDBContext>()
.AddDefaultTokenProviders();
builder.Services.AddScoped<TourRepository>();
builder.Services.AddScoped<ITourService, TourService>();
Here is my Context:
public class TourDBContext : IdentityDbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
SeedRoles(builder);
// Bỏ tiền tố AspNet của các bảng
foreach (var entityType in builder.Model.GetEntityTypes())
{
var tableName = entityType.GetTableName();
if (tableName.StartsWith("AspNet"))
{
entityType.SetTableName(tableName.Substring(6));
}
}
}
private static void SeedRoles(ModelBuilder modelBuilder)
{
var seedRoles = new List<IdentityRole>{
new IdentityRole{ Name = "Admin", ConcurrencyStamp = "1", NormalizedName = "Admin"},
new IdentityRole{ Name = "User", ConcurrencyStamp = "2", NormalizedName = "User"},
new IdentityRole{ Name = "HR", ConcurrencyStamp = "3", NormalizedName = "HumanResources"}
};
modelBuilder.Entity<IdentityRole>().HasData(seedRoles);
}
public TourDBContext()
{
}
public TourDBContext(DbContextOptions<TourDBContext> options) : base(options) { }
public DbSet<Tour> Tours { get; set; }
public DbSet<Review> Reviews { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Image> Images { get; set; }
public DbSet<Booking> Bookings { get; set; }
}
But when I write this in Context, the error does not occur. Will my writing OnConfiguring in DBContext and AddDbContext in Program.cs have any effect?
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(GetConnectionString());
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
}
private string GetConnectionString()
{
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.Build();
var strConn = config["ConnectionStrings:TravelDBConnectionString"];
return strConn;
}
Upvotes: 0
Views: 66
Reputation: 11871
modify your repository to:
public class BaseRepository<T> where T : class
{
private readonly TourDBContext _context;
public BaseRepository(TourDBContext context)
{
_context = context;
}
......
}
Upvotes: 0