尚未为此 DbContext 配置数据库提供程序。可以通过覆盖“DbContext.OnConfiguring”或“AddDbContext”来配置提供程序

问题描述 投票:0回答:1

我有一个错误

尚未为此 DbContext 配置数据库提供程序。可以通过重写“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用“AddDbContext”,则还要确保您的 DbContext 类型接受 DbContextOptions 当我调用这个方法时:

    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>
    {
    }

这是我的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>();

这是我的背景:

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; }
}

但是当我在 Context 中写这个时,错误不会发生。我在Program.cs中的DBContext和AddDbContext中写OnConfiguring有效果吗?

        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;
        }
c# entity-framework asp.net-core asp.net-identity dbcontext
1个回答
0
投票

将您的存储库修改为:

public class BaseRepository<T> where T : class
    {
        private readonly TourDBContext _context;

        public BaseRepository(TourDBContext context)
        {
            _context =  context;
        }

       ......
     }
© www.soinside.com 2019 - 2024. All rights reserved.