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

前端 未结 9 2930
逝去的感伤
逝去的感伤 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 16:57

    The quick solution to the problem is to implement the default constructor in your context

     public MyContext() : base()
     {
     }
    

    The problem with this solution is that you will have to enter the connection string in the 'OnConfiguring' function explicitly, which is not recommended

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite("ConnectionString");
            }
     }
    

提交回复
热议问题