C
C#2y ago
TJFrags

❔ Update-Database causing error

Hi, When I try to use the "Update-Database" command in visual studio an error occures: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Failed to load configuration from file 'C:\Users\Pc\source\repos\GamersRisingWeb\GamersRising\appsettings.json'. Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 This started after changing a migration file but now I still get the error after changing it back. Is there a way to fix this without restarting?
Design-time DbContext Creation - EF Core
Strategies for creating a design-time DbContext with Entity Framework Core
14 Replies
TJFrags
TJFragsOP2y ago
I am using .net 7, asp.net core mvc
Pobiega
Pobiega2y ago
It can't load your appsettings file, which is marked as required by your app host builder Is there an appsettings.json file in the specified location? If so, what's the content in it?
TJFrags
TJFragsOP2y ago
@Pobiega I found a soltion to that. It was caused by some miss-placedcode. now im getting this: Invalid column name 'DateOfBirth'. Invalid column name 'FullName'.
Pobiega
Pobiega2y ago
uhm, that means some of your code is trying to access the database and it doesnt have the field Im assuming you are trying to migrate to
TJFrags
TJFragsOP2y ago
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using GamersRising.Entities; using Microsoft.AspNetCore.Mvc.Formatters; namespace GamersRising.Data { public class ApplicationUser : IdentityUser { [StringLength(250)] public string FullName { get; set; } [StringLength(250)] public string UserName { get; set; } [StringLength(250)] public string Email { get; set; } [StringLength(250)] public string PhoneNumber { get; set; } [StringLength(250)] public string DateOfBirth { get; set; } [ForeignKey("UserId")] public virtual ICollection<UserCatagory>? UserCategory { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Game> Game { get; set; } public DbSet<Tournament> Tournament { get; set; } public DbSet<TournamentType> TournamentType { get; set; } public DbSet<UserCatagory> UserCatagory { get; set; } public DbSet<Content> Content { get; set; } public DbSet<TournamentFormat> TournamentFormat { get; set; } public DbSet<TournamentMode> TournamentMode { get; set; } } }
Pobiega
Pobiega2y ago
okay?
TJFrags
TJFragsOP2y ago
thats the ApplicationDbContext file
Pobiega
Pobiega2y ago
I can see that, yep. Its not the cause of your error thou. when applying migrations, update-database will initialize your db context since it needs to connect to the database for some reason, its also running parts of your application and thats causing exceptions before it finished migrating
TJFrags
TJFragsOP2y ago
Oh I got it Although this doesnt happen when I remove the DateOfBirth and FullName from the
INSERT INTO AspNetUsers
INSERT INTO AspNetUsers
method
Pobiega
Pobiega2y ago
yeah, because then the SQL query works so its still running queries during migration, which is bad
TJFrags
TJFragsOP2y ago
Is there anything else I can do to get these columns in the database?
Pobiega
Pobiega2y ago
you should figure out why your application is running code before the host starts and then stop that and then run the migration
TJFrags
TJFragsOP2y ago
Alright I got it, THanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server