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

前端 未结 9 2819
逝去的感伤
逝去的感伤 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:54

    Kindly check your connection string also in appsetting,json.

    0 讨论(0)
  • 2021-02-19 16:55

    I came across this problem today. In my case, the SqlDbContext was in a separate ASP.Net Core 3.1 class library project, and I was trying to setup migrations using the dotnet CLI from that project's root folder. The main web application, which is the default project, contains the connection string configuration inside the appsettings.json and the startup configurations therefore I had to specify the startup project path using the -s switch as follows.

    >dotnet ef migrations add initialcreation -s ..\MyWebApp\MyWebApp.csproj
    

    -s, short for startup project, is a quick alternative to implementing IDesignTimeDbContextFactory when the DbContext is in a different project than the web application project.

    0 讨论(0)
  • 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");
            }
     }
    
    0 讨论(0)
  • 2021-02-19 16:58

    Hade the same problem with NET Core 3.1.

    Needed to add one more constructor too solev this. Why I do not know.

    Never hade to do it in the past.

    public DataContext()
    {            
    }
    
    public DataContext(DbContextOptions<DataContext> options)
        : base(options)
    { }
    
    0 讨论(0)
  • 2021-02-19 16:59

    I've had same problem as You. Maybe it was not for a Console Application but error was the same. So i thought that it is worth to share with my answer. I was using NET Core 3.0 and to fix the problem I have to change the IHostBuilder into IWebHost and then everything was fine. The problem was in class Program.

    public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    

    into

    public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
    
    0 讨论(0)
  • 2021-02-19 17:05

    In my case, I had two startup project in my solution, so setting the project that has the connection string as the only startup project fixed the issue

    0 讨论(0)
提交回复
热议问题