.net - 'ApplicationDbContext' 不继承自 DbContext
问题描述
我收到此错误:
该类型ApplicationDbContext
不继承自DbContext
. 该DbMigrationsConfiguration.ContextType
属性必须设置为继承自 的类型DbContext
。
这是我的代码:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
//using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
namespace Manufactura.Models
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Categories> Categories { get; set; }
public DbSet<CustomerCities> CustomerCities { get; set; }
public DbSet<CustomerCountries> CustomerCountries { get; set; }
public DbSet<CustomerRegions> CustomerRegions { get; set; }
public DbSet<Customers> Customers { get; set; }
public DbSet<Employees> Employees { get; set; }
public DbSet<Payments> Payments { get; set; }
public DbSet<ProductDetails> ProductDetails { get; set; }
public DbSet<ProductionOrders> ProductionOrders { get; set; }
public DbSet<Products> Products { get; set; }
public DbSet<RawMaterials> RawMaterials { get; set; }
public DbSet<SaleInvoices> SaleInvoices { get; set; }
public DbSet<Shippings> Shippings { get; set; }
public DbSet<SuplierCities> SuplierCities { get; set; }
public DbSet<SuplierCountries> SuplierCountries { get; set; }
public DbSet<SuplierInvoices> SuplierInvoices { get; set; }
public DbSet<SuplierRegions> SuplierRegions { get; set; }
public DbSet<Supliers> Supliers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Categories>().HasKey(m => m.CategoryId);
modelBuilder.Entity<CustomerCities>().HasKey(m => m.CustomerCity);
modelBuilder.Entity<CustomerCountries>().HasKey(m => m.CustomerCountry);
modelBuilder.Entity<CustomerRegions>().HasKey(m => m.CustomerRegion);
modelBuilder.Entity<Customers>().HasKey(m => m.CustomerId);
modelBuilder.Entity<Employees>().HasKey(m => m.EmployeeId);
modelBuilder.Entity<Payments>().HasKey(m => m.PaymentId);
modelBuilder.Entity<ProductDetails>().HasKey(m => m.ProductDetailsId);
modelBuilder.Entity<ProductionOrders>().HasKey(m => m.ProductionOrdersId);
modelBuilder.Entity<Products>().HasKey(m => m.ProductId);
modelBuilder.Entity<RawMaterials>().HasKey(m => m.RawMaterialsId);
modelBuilder.Entity<SaleInvoices>().HasKey(m => m.SaleInvoiceId);
modelBuilder.Entity<Shippings>().HasKey(m => m.ShippingId);
modelBuilder.Entity<SuplierCities>().HasKey(m => m.SuplierCity);
modelBuilder.Entity<SuplierCountries>().HasKey(m => m.SuplierCountry);
modelBuilder.Entity<SuplierInvoices>().HasKey(m => m.SuplierInvoiceId);
modelBuilder.Entity<SuplierRegions>().HasKey(m => m.SuplierRegion);
modelBuilder.Entity<Supliers>().HasKey(m => m.SuplierId);
modelBuilder.Entity<Products>()
.HasOne(p => p.Category)
.WithMany(b => b.Products);
modelBuilder.Entity<CustomerRegions>()
.HasOne(p => p.CustomerCountry)
.WithMany(b => b.CustomerRegions);
modelBuilder.Entity<CustomerCities>()
.HasOne(p => p.CustomerRegion)
.WithMany(b => b.CustomerCities);
modelBuilder.Entity<Customers>()
.HasOne(p => p.CustomerCity)
.WithMany(b => b.Customers);
modelBuilder.Entity<SaleInvoices>()
.HasOne(p => p.Customer)
.WithMany(b => b.SaleInvoices);
modelBuilder.Entity<Payments>()
.HasOne(p => p.SaleInvoice)
.WithMany(b => b.Payments);
modelBuilder.Entity<ProductDetails>()
.HasOne(p => p.SaleInvoice)
.WithMany(b => b.ProductDetails);
modelBuilder.Entity<Products>()
.HasOne(p => p.ProductDetail)
.WithMany(b => b.Products);
modelBuilder.Entity<Products>()
.HasOne(p => p.ProductionOrder)
.WithMany(b => b.Products);
modelBuilder.Entity<Products>()
.HasOne(p => p.Employee)
.WithMany(b => b.Products);
modelBuilder.Entity<Products>()
.HasOne(p => p.Category)
.WithMany(b => b.Products);
modelBuilder.Entity<ProductionOrders>()
.HasOne(p => p.RawMaterial)
.WithMany(b => b.ProductionOrders);
modelBuilder.Entity<SuplierInvoices>()
.HasOne(p => p.RawMaterial)
.WithMany(b => b.SuplierInvoices);
modelBuilder.Entity<SuplierInvoices>()
.HasOne(p => p.Suplier)
.WithMany(b => b.SuplierInvoices);
modelBuilder.Entity<Supliers>()
.HasOne(p => p.SuplierCity)
.WithMany(b => b.Supliers);
modelBuilder.Entity<SuplierCities>()
.HasOne(p => p.SuplierRegion)
.WithMany(b => b.SuplierCities);
modelBuilder.Entity<SuplierRegions>()
.HasOne(p => p.SuplierCountry)
.WithMany(b => b.SuplierRegions);
base.OnModelCreating(modelBuilder);
}
}
}
我已经尝试了该网站所说的所有内容,包括重新启动 VS:
enable-migrations -ProjectName Manufactura -Verbose
Enable-Migrations -ProjectName Manufactura -ContextTypeName Manufactura.Models.ApplicationDbContext
Enable-Migrations -ProjectName Manufactura -ContextTypeName Manufactura.Models.ApplicationDbContext -force
Install-Package EntityFramework -IncludePrerelease
UnInstall-Package EntityFramework
Install-Package EntityFramework
这是.Net Core 3.1在VS 2019中的应用
如果我取消注释System.Data.Entity
,我会收到错误:DbContext
是一个模棱两可的参考
没有任何效果。
这是我的应用设置
{
"ConnectionStrings": {
"ConnectionString": "Data Source=.;Initial catalog=confecciones;Integrated Security=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
在 DbContext 上使用 f12,它带我到这里:
namespace Microsoft.EntityFrameworkCore
ublic class DbContext : IDisposable, IAsyncDisposable, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable, IResettableService
{
//
// Resumen:
// Initializes a new instance of the Microsoft.EntityFrameworkCore.DbContext class
// using the specified options. The Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)
// method will still be called to allow further configuration of the options.
//
// Parámetros:
// options:
// The options for this context.
public DbContext([NotNullAttribute] DbContextOptions options);
//
// Resumen:
// Initializes a new instance of the Microsoft.EntityFrameworkCore.DbContext class.
// The Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)
// method will be called to configure the database (and other options) to be used
// for this context.
protected DbContext();
启动代码:
using System;
using System.Collections.Generic;
using System.Linq;
//using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Manufactura.Models;
using Microsoft.Extensions.Hosting;
namespace Manufactura
{
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.AddRazorPages();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
services.AddControllers();
}
// 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("/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.MapRazorPages();
});
}
}
}
应用设置:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"ConnectionString": "Data Source=DESKTOP-APHO8VE\\SQLEXPRESS;Initial catalog=confecciones;Integrated Security=true"
}
}
解决方案
在@LJH 说之后,我一遍又一遍地使用相同的名称进行迁移,我已经更改了它并且我使用了太短版本的 add-migration 现在它可以工作了
推荐阅读
- php - apache2 没有在 Debian 上执行 php 脚本
- c++ - 是否可以为 std::array 类型添加自己的构造函数?
- python - heapq,处理两个对象的相同值?
- c# - 在 Unity(C#) 中处理正在运行的演示文稿的输出
- ms-access - MS Access 设置 RecordSource 导致错误
- asp.net-core - Serilog 和 ASP.NET Core 丰富了 `WithUserName` 和 `WithHttpRequestUserAgent`?
- c# - 将@Html.ActionLink 与@Html raw 一起使用
- mongodb - 使用python插入后Mongodb未连接
- javascript - 以编程方式单击 div 根以打开使用 java 脚本在内部打开的内容
- python - 仅按 QSortFilterProxyModel 中的顶级项过滤