Witam serdecznie. Stworzyłem DependencyInjection
w projekcie WPF netCore. Wszystko działa dobrze. Teraz utworzyłem klasę DbContext
aby ustanowić połączenie z baza danych:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
public partial class App : Application
{
private readonly ServiceProvider _serviceProvider;
public App()
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
_serviceProvider = serviceCollection.BuildServiceProvider();
}
private void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
"(LocalDb)/MSSQLLocalDB"));
services.AddSingleton<IAppSettingsService, AppSettingsService>();
services.AddSingleton<MainWindow>();
}
private void OnStartup(object sender, StartupEventArgs e)
{
var mainWindow = _serviceProvider.GetService<MainWindow>();
mainWindow.Show();
}
}
Niestety podczas komendy add-migration
otrzymuję błąd:
PM> add-migration initial
Build started...
Build succeeded.
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Jest o tym sporo w internecie, ale nie pomagają sposoby opisane w rozwiązaniach. Możecie mi wytłumaczyć, co może być nie tak?