C
C#8mo ago
Juliandyce

✅ How to automate update-database

I am bilding a Web server but i have to typ in update-database how can i automate this in Blazor in the Program.cs file. Im srry i cant be more specific because im new in this kind of stuff. The tutorial i watch is just outdated the code changed a bit. Tanks for your time and helping. In the tutorial https://www.youtube.com/watch?v=JeLnR_ObHd8&t=4624s on 1:16:57 - 1:17:17 and i have the most recent Version of Microsoft Visual Studio if u also have it and it looks like that: var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); }
Coding mit Jannick
YouTube
Erstelle und hoste eine Blazor Server App mit ASP.NET Core, VS 2022...
In diesem Video erstellst du deine erste Blazor Server App vom Scratch bis hin zur Veröffentlichung im IIS auf einem Windows Server. 🔥 Hier geht's zum Contabo Windows Server Angebot: https://bit.ly/3IOlzOT SQL Server Express 2019: https://www.microsoft.com/de-DE/download/details.aspx?id=101064 ASP.NET Core 5.0 Hosting Bundle (erst SQL Server ...
17 Replies
Jimmacle
Jimmacle8mo ago
if you get an instance of your dbcontext you can call dbContext.Database.Migrate()
Juliandyce
Juliandyce8mo ago
i wanna updat sql datenbanken databanks
Jimmacle
Jimmacle8mo ago
the method i suggested does the same thing as update-database
Juliandyce
Juliandyce8mo ago
if u know where i have to put it because as u wrote i cant put it in the Program.cs file how can i put code in this extra blackish window on discord
Pobiega
Pobiega8mo ago
$codegif
Juliandyce
Juliandyce8mo ago
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
builder.Services.AddScoped<ProductService>();

var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
builder.Services.AddScoped<ProductService>();

var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
this is how the program.cs looks like
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=PlanerApp;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=PlanerApp;Trusted_Connection=True;MultipleActiveResultSets=true"
Jimmacle
Jimmacle8mo ago
var app = builder.Build();
using (var serviceScope = app.Services.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
var app = builder.Build();
using (var serviceScope = app.Services.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
should be roughly that, i typed it in discord so no compiler checking you need a service scope because .AddDbContext registers the dbcontext as a scoped service an alternative is .AddDbContextFactory and using IDbContextFactory<ApplicationDbContext>
Juliandyce
Juliandyce8mo ago
"IServiceScope" has no definition for "GetRequiredService", and the overload of the optimal extension method "ServiceProviderServiceExtensions.GetRequiredService<ApplicationDbContext>(IServiceProvider)" needs a receiver of the typ "System.IServiceProvider".
Jimmacle
Jimmacle8mo ago
corrected
Juliandyce
Juliandyce8mo ago
what now
Jimmacle
Jimmacle8mo ago
use the updated code i edited the message
Juliandyce
Juliandyce8mo ago
oh thank u it now at least works. I dont know if it does what I want but 🫨 . I hope I didn´t distract u with my silly question.
Jimmacle
Jimmacle8mo ago
it does do what you want you want to do update-database but in code right? this will apply migrations automatically when your app starts
Juliandyce
Juliandyce8mo ago
je so when i take it online it does it automaticly
Want results from more Discord servers?
Add your server