1

I am working on an application where I need to add migrations but it says unable to add migrations. I am registering startup.cs but CreateDefaultBuilder doesn't contain definition of ConfigureWebHostDefaults.

Here is my code:

IConfiguration configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .Build();

using var host = Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
    .Build();

It is a console application. In the startup class I am registering dbContext like this:

collection.AddDbContext<DataScrapperContext>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

How to use code-first approach to add migrations using console application in .NET Core 6.0?

1 Answer 1

2

I have resolve my problem by doing like this. I didn't go with startup class here is my code and my register class and it work like a charm. My program.cs consist of the following code.

//Build configuration instance
IConfiguration configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .Build();

//Build logger for the app
Log.Logger = new LoggerConfiguration()
          .ReadFrom.Configuration(configuration)
          .CreateLogger();

//Create host & configure services to use in the app
using IHost host = Host.CreateDefaultBuilder(args)
   .UseSerilog()
    .ConfigureServices(services =>
    {
        services.ConfigureApplicationServices(configuration);
    })
   .Build();

Here is my ConfigureApplicationServices class code. I have also configure Serilog in between.

public static class ApplicationService
    {
        /// <summary>
        /// Registering all the application services with our DI container
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="configuration"></param>
        public static void ConfigureApplicationServices(this IServiceCollection collection, IConfiguration configuration)
        {

            #region [SERVICES REGISTERATIONs]
            collection.AddSingleton(configuration);
            collection.AddScoped<ICoordinateService, CoordinateService>();
            collection.AddScoped<IResponseService, ResponseService>();
            collection.AddScoped<RestClientService>();
            collection.AddScoped<CoordinatesProccessor>();
            #endregion

            collection.AddDbContext<DataScrapperContext>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

        }
    }

I have added my migrations with code first approach. Might this will be helpful for someone using console application with .NET Core 6.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.