❔ 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
I am using .net 7, asp.net core mvc
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?
@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'.
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
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; }
}
}
okay?
thats the ApplicationDbContext file
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 migratingOh I got it
Although this doesnt happen when I remove the DateOfBirth and FullName from the method
yeah, because then the SQL query works
so its still running queries during migration, which is bad
Is there anything else I can do to get these columns in the database?
you should figure out why your application is running code before the host starts
and then stop that
and then run the migration
Alright I got it, THanks
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.