Update Program.cs for Entity Framework #103

Closed
opened 2025-08-09 17:15:01 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @johankvint on 4/11/2018

Improvement:

Hi,
I was using dotnet new angular to generate a dotnet core angular template with SPA-templates version 2.
The layout of Programs.cs was the following:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

I added Entity Framework to this solution and got the following error when running any dotnet ef [command]

Unable to create an object of type 'DataContext'. Add an implementation of 'IDesignTimeDbContextFactory<DataContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

It took me a really long time to figure out what was wrong, but just changing Program.cs to the following (according to https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/?view=aspnetcore-2.1#update-main-method-in-programcs):

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

solved that problem

*Originally created by @johankvint on 4/11/2018* Improvement: Hi, I was using `dotnet new angular` to generate a dotnet core angular template with SPA-templates version 2. The layout of Programs.cs was the following: ``` public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } ``` I added Entity Framework to this solution and got the following error when running any `dotnet ef [command]` ``` Unable to create an object of type 'DataContext'. Add an implementation of 'IDesignTimeDbContextFactory<DataContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. ``` It took me a really long time to figure out what was wrong, but just changing Program.cs to the following (according to https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/?view=aspnetcore-2.1#update-main-method-in-programcs): ``` public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); } ``` solved that problem
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#103
No description provided.