在MVC Core中使用现有数据库时出现InvalidOperationException

时间:2019-04-24 07:43:06

标签: asp.net-core-mvc entity-framework-core

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

  

处理以下内容时发生未处理的异常   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);
                });
            }
        }
    }

用户存储库

    namespace BookStore.Models
    {
        public class UsersRepo : IUser
        {
            private readonly BookStoreContext _bookStoreContext;

            public UsersRepo(BookStoreContext bookStoreContext)
            {
                _bookStoreContext = bookStoreContext;
            }

            public void AddUser(Users users)
            {
                _bookStoreContext.Users.Add(users);
                _bookStoreContext.SaveChanges();
            }
        }
    }

用户模型

    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();
            }
        }

1 个答案:

答案 0 :(得分:0)

我尝试了这个,它很正常

 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";
        });
    }