访问 Microsoft.extension.hosting 服务时添加初始迁移后出现错误

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

添加第一个迁移 Add-Migration InitialMigration 后,它会在下面显示此错误,

访问 Microsoft.Extensions.Hosting 服务时发生错误。在没有应用程序服务提供商的情况下继续。错误:无法从文件“C:\Users\Ammad\source”加载配置 epos\EmployeesProject\EmployeesProject ppsettings.json'。 无法创建“ApplicationDbContext”类型的“DbContext”。尝试激活“EmployeesProject.Context.ApplicationDbContext”时出现异常“无法解析类型“Microsoft.EntityFrameworkCore.DbContextOptions`1[EmployeesProject.Context.ApplicationDbContext]”的服务。”尝试创建实例时抛出。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728

请让我知道解决方案,因为我需要添加第一次迁移。谢谢

请在下面找到我的代码,仅创建一个简单的类 Employee.cs

namespace EmployeesProject.Models
{
    public class Employee
    {

        public int Id { get; set; }

        public string FirstName { get; set; }
        public string MiddleName { get; set; }

        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public int PhoneNumber { get; set; }
    }
}

我的应用程序数据库上下文类代码如下所述

using EmployeesProject.Models;
using Microsoft.EntityFrameworkCore;

namespace EmployeesProject.Context
{
    public class ApplicationDbContext : DbContext
    {

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext>contextOptions): base(contextOptions)
        {

        }

        // Code First Approach
        public DbSet<Employee> Employees { get; set; }
    }
}

appsetting.json 代码如下

{

     "ConnectionStrings": {
        "DefaultConnection": "Server": "DESKTOP-DFBVCE0;Database:Employees; User Id=Ammadlogin;Password:ammadpass1; TrueServerCertificate:True"
      },
    
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*"
    }

请找到下面的Program.cs,

namespace Employeeproject
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                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.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}
asp.net asp.net-mvc asp.net-core entity-framework-core
1个回答
0
投票

你的 ConnectionStrings 应该是这样的:

 "ConnectionStrings": {"DefaultConnection": "Server={DBAddress};Database={DBName};User Id={user};Password={Pass};MultipleActiveResultSets=true"},

你的 ApplicationDbContext 应该是这样的:

public MDBContext(IConfiguration config, DbContextOptions<MDBContext> options) : base(options)
{
    appConfig = config;
}
public DbSet<testdb> Mytest { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    if (!options.IsConfigured)
    {
    options.UseSqlServer(appConfig.GetConnectionString("NameOFYourCNStringInAppsettings"));

    }

}

最好回顾一下: https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

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