Asp.net core mvc 在MVC Core中使用现有数据库时出现InvalidOperationException

Asp.net core mvc 在MVC Core中使用现有数据库时出现InvalidOperationException,asp.net-core-mvc,entity-framework-core,Asp.net Core Mvc,Entity Framework Core,我试图在我的应用程序中使用现有的数据库,但每次我点击视图时,它都会显示 处理时发生未处理的异常 InvalidOperationException:无法解析类型的服务 尝试激活时出现“BookStore.Models.BookStoreContext” 'BookStore.Models.UsersRepo' 上下文 namespace BookStore.Models { public partial class BookStoreContext : DbCon

我试图在我的应用程序中使用现有的数据库,但每次我点击视图时,它都会显示

处理时发生未处理的异常 InvalidOperationException:无法解析类型的服务 尝试激活时出现“BookStore.Models.BookStoreContext” 'BookStore.Models.UsersRepo'

上下文

    namespace BookStore.Models
    {
        public partial class BookStoreContext : DbContext
        {
            public BookStoreContext()
            {
            }

            public BookStoreContext(DbContextOptions<BookStoreContext> options)
                : base(options)
            {
            }

            public virtual DbSet<Users> Users { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer("Server=(localdb)\\V11.0;Database=BookStore;Trusted_Connection=True;");
                }
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.HasAnnotation("ProductVersion", "2.2.3-servicing-35854");

                modelBuilder.Entity<Users>(entity =>
                {
                    entity.HasKey(e => e.UserId);

                    entity.Property(e => e.UserId).HasColumnName("User_ID");

                    entity.Property(e => e.Password)
                        .IsRequired()
                        .HasMaxLength(50);

                    entity.Property(e => e.UserName)
                        .IsRequired()
                        .HasColumnName("User_Name")
                        .HasMaxLength(50);
                });
            }
        }
    }
用户模型

    public partial class Users
        {
            public long UserId { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
            public int Type { get; set; }
        }


     public interface IUser
        {
            void AddUser(Users users);
        }
用户控制器

    public class UsersController : Controller
        {
            private readonly IUser _userRepo;

            public UsersController(IUser userRepo)
            {
                _userRepo = userRepo;
            }

            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }

            [HttpPost]
            public IActionResult Index(Users users)
            {
                _userRepo.AddUser(users);

                return RedirectToAction("UserAddedSuccessfully");
            }

            public IActionResult UserAddedSuccessfully()
            {
                return View();
            }
        }

我试过这个,效果很好

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddDbContext<BookStoreContext>();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext();
//在生产中,角度文件将从此目录中提供
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“ClientApp/dist”;
});
}

我试过这个,效果非常好

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddDbContext<BookStoreContext>();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext();
//在生产中,角度文件将从此目录中提供
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“ClientApp/dist”;
});
}

您是否使用实体框架核心?如果不是这样,这将不起作用。如果您试图将现有的EF 5/6代码与.net Core一起使用,它将无法工作,因为两者不兼容。请参见您是否通过
services.AddDbContext()注册了dbcontext?@itminus我在Startup.cs中没有这样做。谢谢。您使用的是实体框架核心吗?如果不是这样,这将不起作用。如果您试图将现有的EF 5/6代码与.net Core一起使用,它将无法工作,因为两者不兼容。请参见您是否通过
services.AddDbContext()注册了dbcontext?@itminus我在Startup.cs中没有这样做。非常感谢。