Unable to create an object of type 'MyContext'. For the different patterns supported at design time

前端 未结 9 2927
逝去的感伤
逝去的感伤 2021-02-19 16:42

I have ConsoleApplication on .NET Core and also i added my DbContext to dependencies, but howewer i have an error:

Unable to create an object of type \'My

9条回答
  •  余生分开走
    2021-02-19 17:21

    I Resolved this by just adding a plain constructor to my Context

    public class DataContext : DbContext
    {
        public DataContext()
        {
        }
    
        public DataContext(DbContextOptions options) : base(options)
        {
        }
    
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            if (!options.IsConfigured)
            {
                options.UseSqlServer("A FALLBACK CONNECTION STRING");
            }
        }
        
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);            
        }
    }
    

提交回复
热议问题