1

I tried to use the existing database in my application but every time I hit the view it says

An unhandled exception occurred while processing the InvalidOperationException: Unable to resolve service for type 'BookStore.Models.BookStoreContext' while attempting to activate 'BookStore.Models.UsersRepo'

Context

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

User Repository

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

User Model

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

User Controller

    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();
            }
        }
3
  • Are you using Entity Framework Core? If not this will not work. If you're trying to use existing EF 5/6 code with .net Core it will not work as the two are not compatible. See stackoverflow.com/questions/43746738/… Commented Apr 24, 2019 at 8:18
  • 1
    Have you registered the dbcontext by services.AddDbContext<BookStoreContext>();? Commented Apr 24, 2019 at 8:21
  • @itminus I haven't done this in Startup.cs . Thank you. Commented Apr 24, 2019 at 9:18

1 Answer 1

0

I tried this one and it is perfectly working

 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";
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.