Unable to resolve service for type while attempting to activate

后端 未结 7 960
春和景丽
春和景丽 2020-12-09 02:22

I have one file Repository.cs that contains an interface and its implementation like so:

public interface IRepository

{
    IEnumerable

        
7条回答
  •  庸人自扰
    2020-12-09 02:59

    We are getting this error in Entity frame work core database first approach. I followed below steps and error got resolvedenter code here

    Step 1: Check Your context class constructor should be like this

    public partial class ZPHSContext : DbContext
    {
        public ZPHSContext(DbContextOptions dbContextOptions):base(dbContextOptions)
        {
        }
    }
    

    Step 2: In Startup file

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
    }
    

    Step 3: Connection string in appsettings

    "ConnectionStrings": {
        "BloggingDatabase": "Server=Server=****;Database=ZPHSS;Trusted_Connection=True;"
    }
    

    Step 4: Remove default code in OnConfiguring method in context class

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    }
    

提交回复
热议问题