Bob*_*nik 33 .net c# dbcontext asp.net-core
我遇到了一个我自己无法解决的问题,所以我寻求帮助。
\n我最近开始学习 ASP.NET Core,想要构建一个库,其中我必须拥有数据库、登录系统和管理员权限,以便能够从网站添加和删除书籍。
\n我已经创建了登录系统和数据库,现在我想将CRUD添加到项目中,但弹出此错误:
\n“\'尝试激活“LibraryData.LibraryContext”时无法解析类型 \xc2\xa8Microsoft.entityFrameworkCore.DbContextOptions\xc2\xa81[LibraryData.LibraryContext] 的服务
\n这是我到目前为止的代码......
\npublic class LibraryContext : DbContext\n{\n public LibraryContext(DbContextOptions<LibraryContext> options) : base(options)\n {\n\n }\n\n public DbSet<User> Users { get; set; }\n public DbSet<Video> Videos { get; set; }\n public DbSet<BranchHours> BranchHour { get; set; }\n public DbSet<Checkout> checkouts { get; set; }\n public DbSet<CheckoutHistory> checkoutHistorys { get; set; }\n public DbSet<Holds> holds { get; set; }\n public DbSet<LibraryAsset> LibraryAssets { get; set; }\n public DbSet<Book> Books { get; set; }\n public DbSet<LibraryBranch> libraryBranches { get; set; }\n public DbSet<LibraryCard> LibraryCards { get; set; }\n public DbSet<Status> statuses { get; set; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n public IConfiguration Configuration { get; }\n\n public Startup(IConfiguration configuration)\n {\n Configuration = configuration;\n }\n\n public void ConfigureServices(IServiceCollection services)\n {\n services.AddMvc();\n services.AddMvc().AddSessionStateTempDataProvider();\n services.AddDistributedMemoryCache();\n services.AddSession();\n services.AddSingleton(Configuration);\n services.AddScoped<ILibraryAsset, LibraryAssetService>();\n services.AddControllersWithViews();\n services.AddDbContext<LibraryContext>(options\n => options.UseSqlServer(Configuration.GetConnectionString("LibraryConnection")));\n services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<LibraryContext>();\n services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();\n services.AddScoped<DbContext, LibraryContext>();\n services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();\n }\n\n public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n {\n if (env.IsDevelopment())\n {\n app.UseDeveloperExceptionPage();\n }\n else\n {\n app.UseExceptionHandler("/Home/Error");\n app.UseHsts();\n }\n app.UseHttpsRedirection();\n app.UseStaticFiles();\n app.UseSession();\n app.UseRouting();\n app.UseAuthorization();\n\n app.UseEndpoints(endpoints =>\n {\n endpoints.MapControllerRoute(\n name: "default",\n pattern: "{controller=Home}/{action=Index}/{id?}");\n });\n }\n
Run Code Online (Sandbox Code Playgroud)\npublic class Program\n{\n public static void Main(string[] args)\n {\n CreateHostBuilder(args).Build().Run();\n }\n\n public static IHostBuilder CreateHostBuilder(string[] args) =>\n Host.CreateDefaultBuilder(args)\n .ConfigureWebHostDefaults(webBuilder =>\n {\n webBuilder.UseStartup<Startup>();\n });\n}\n
Run Code Online (Sandbox Code Playgroud)\npublic class AssetIndexListingModel\n{\n public int Id { get; set; }\n public string ImageUrl { get; set; }\n public string Title { get; set; }\n public string AuthorOrDirector { get; set; }\n public string Type { get; set; }\n public string DeweyCallNumber { get; set; }\n public string NumberOfPages { get; set; }\n public int DateOfRealease { get; set; }\n public int AgeRes { get; set; }\n public string About { get; set; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
Rec*_*diz 50
我通过为Entity Framework Core CLI 工具指定--project和--startup-project选项修复了此错误,如下所示:
dotnet ef database update --verbose --project CommandService.Data --startup-project CommandService
Run Code Online (Sandbox Code Playgroud)
--project表示哪个项目包含DbContext类
--startup-project表示哪个项目包含数据库连接信息等信息。
She*_*jid 15
我最近遇到了这个问题,这就是我解决这个问题的方法。
在 Startup.cs(配置服务)中:
public void ConfigureServices(IServiceCollection services)
{
//Some Code
services.AddDbContext<LibraryContext>();
//Some Code
}
Run Code Online (Sandbox Code Playgroud)
然后在 DbContext 类中,将 DbContextOptions 传递给基类,按如下方式配置它们。
public LibraryContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Your Connection String");
}
Run Code Online (Sandbox Code Playgroud)
更新
这样做的原因是,EF Core Tools 仅支持活动提供程序的脚手架迁移,如果您正在创建一个 SDK 样式项目,其中迁移程序集与活动程序集不同,则可以通过这种方式覆盖配置
小智 8
只需为 DbContext 添加一个空构造函数即可解决您的问题:
public partial class TPPSSDbContext : DbContext
{
public TPPSSDbContext() { } // This one
public TPPSSDbContext(DbContextOptions<TPPSSDbContext> options)
: base(options)
{
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您应该添加告诉工具应如何创建 DBContext 的代码。当类库项目中有 DBContext 引用到 Web 项目时,就需要它。
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseNpgsql();
return new BloggingContext(optionsBuilder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
57435 次 |
最近记录: |