Visual Studio 无法创建类型为“applicationdbcontext”的对象,对于设计时支持的不同模式错误

l7wslrjt  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(61)

当我尝试添加迁移时,此错误不断弹出:
无法为设计时支持的不同模式创建类型为“applicationdbcontext”的对象
Startup.cs代码:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

字符串
ApplicationDBContext.cs代码:

public class ApplicationDBContext : DbContext
{
    public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
        : base(options)
    {
    }
    public DbSet<Item> Items { get; set; }
}

4ioopgfo

4ioopgfo1#

ASP.NET Core应用程序使用依赖项注入进行配置。EF Core可以使用Startup.cs的ConfigureServices方法中的AddDbContext添加到此配置。例如:

services.AddDbContext<ApplicationDbContext>(
        options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));

字符串
您也可以使用简单DbContext初始化与'新'初始化它

public class ApplicationDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"YourConnectionStringGoesHere");
    }
}

cl25kdpy

cl25kdpy2#

在AddDbContext之后添加var app = builder.Build();


的数据

相关问题