I need to preface this with pointing out this is my first web app project. I am trying to make a Blazor web application with ASP.NET Core.
When I create a new project and run the code, any database interaction throws an error, the first one is in relation to the database does not exist, if I create a database with the name it looks for, it can't find the tables etc.
I can find the migrations under the migrations directory so they have definitely been generated. I don't understand why the database isn't being created.
This is the connection string:
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-2baf3df8-f795-4732-b452-3cd5855e64a4;Trusted_Connection=True;MultipleActiveResultSets=true"This is a section of the code from the migrations that appears to setup the database and create the tables etc.
modelBuilder.Entity("WebAppplication1.Net.Data.ApplicationUser", b => { b.Property<string>("Id") .HasColumnType("nvarchar(450)"); b.Property<int>("AccessFailedCount") .HasColumnType("int"); b.Property<string>("ConcurrencyStamp") .IsConcurrencyToken() .HasColumnType("nvarchar(max)"); b.Property<string>("Email") .HasMaxLength(256) .HasColumnType("nvarchar(256)");And here is the Program.cs file. It appears to have a few lines setting up the database connection.
using Microsoft.AspNetCore.Components.Authorization;using Microsoft.AspNetCore.Identity;using Microsoft.EntityFrameworkCore;using MudBlazor.Services;var builder = WebApplication.CreateBuilder(args);// Add MudBlazor servicesbuilder.Services.AddMudServices();// Add services to the container.builder.Services.AddRazorComponents() .AddInteractiveServerComponents();builder.Services.AddCascadingAuthenticationState();builder.Services.AddScoped<IdentityUserAccessor>();builder.Services.AddScoped<IdentityRedirectManager>();builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();builder.Services.AddAuthentication(options => { options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme; }) .AddIdentityCookies();var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>() .AddSignInManager() .AddDefaultTokenProviders();builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){ app.UseMigrationsEndPoint();}else{ app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts();}app.UseHttpsRedirection();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();// Add additional endpoints required by the Identity /Account Razor components.app.MapAdditionalIdentityEndpoints();app.Run();Specifically looking at these lines as likely to be the ones setting up the database:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>() .AddSignInManager() .AddDefaultTokenProviders();builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();var app = builder.Build();The error I am getting is this:
Microsoft.Data.SqlClient.SqlException: Invalid object name 'AspNetUsers'