0

I am using .NET 5 and getting this error when I try to add a column to my database. The only fixes I found so far was to set the right startup project, but since I dont have two projects I dont know how to fix it.

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Invalid column name 'EspStartTime'. Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

ApplicationDbContext

internal class TaskEntityTypeConfiguration : IEntityTypeConfiguration<Task>
    {
        public void Configure(EntityTypeBuilder<Task> builder)
        {
            builder.Property(e => e.Description)
                .HasConversion(
                    xml => xml.ToString(),
                    xml => xml != null ? XElement.Parse(xml) : null)
                .HasColumnType("xml");
        }
    }
    internal class StudentAssignedTaskEntityTypeConfiguration : IEntityTypeConfiguration<StudentAssignment>
    {
        public void Configure(EntityTypeBuilder<StudentAssignment> builder)
        {
            builder.Property(e => e.FeedbackXml)
                .HasConversion(
                    xml => xml.ToString(),
                    xml => xml != null ? XElement.Parse(xml) : null)
                .HasColumnType("xml");
            builder.Property(e => e.StudentSubmissionAttachmentsXml)
                .HasConversion(
                    xml => xml.ToString(),
                    xml => xml != null ? XElement.Parse(xml) : null)
                .HasColumnType("xml");
            builder.Property(e => e.StudentSubmissionXml)
                .HasConversion(
                    xml => xml.ToString(),
                    xml => xml != null ? XElement.Parse(xml) : null)
                .HasColumnType("xml");
        }
    }
    public class ApplicationDbContext : IdentityApplicationDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            //this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        public static readonly ILoggerFactory Factory
            = LoggerFactory.Create(builder => { builder.AddConsole(); });

        

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseLoggerFactory(Factory);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
                relationship.DeleteBehavior = DeleteBehavior.Restrict;

            builder.Entity<ApplicationUser>().HasMany(x => x.TeacherGroups).WithOne(x => x.Teacher).OnDelete(DeleteBehavior.SetNull);
            builder.Entity<ApplicationUser>().HasMany(x => x.StudentGroups).WithMany(x => x.Students);
            builder.Entity<SchoolClassStudentTerm>().HasKey(x => new { x.StudentId, x.SchoolClassId, x.TermId });
            builder.ApplyConfiguration(new TaskEntityTypeConfiguration());
            builder.ApplyConfiguration(new StudentAssignedTaskEntityTypeConfiguration());


            builder.Entity<GroupAssignment>().HasOne(x => x.ClassroomGroup).WithMany(x => x.GroupAssignments).OnDelete(DeleteBehavior.Cascade);
            builder.Entity<GroupAssignment>().HasMany(x => x.StudentAssignments).WithOne(x => x.GroupAssignment).OnDelete(DeleteBehavior.Cascade);
            builder.Entity<GroupAssignment>().HasOne(x => x.Task).WithMany(x => x.GroupAssignments).OnDelete(DeleteBehavior.Cascade);
            builder.Entity<SchoolClassStudentTerm>().HasOne(x => x.Student).WithMany(x => x.SchoolClassStudentTerms).OnDelete(DeleteBehavior.Cascade);
            builder.Entity<StudentAssignment>().HasOne(x => x.Student).WithMany().OnDelete(DeleteBehavior.SetNull);
            builder.Entity<GroupAssignment>().HasOne(x => x.Creator).WithMany(x => x.CreatedGroupAssignments).OnDelete(DeleteBehavior.SetNull);

            builder.Entity<SchoolClass>().HasMany(x => x.SchoolClassStudentTerms).WithOne(x => x.SchoolClass);

            // see https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-5.0#add-identityuser-poco-navigation-properties
            builder.Entity<ApplicationUser>().HasMany(x => x.UserRoles).WithOne(x => x.User)
                .HasForeignKey(x => x.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
        }
        public DbSet<SchoolClassStudentTerm> SchoolClassStudentTerms { get; set; }
        public DbSet<Term> Terms { get; set; }
        public DbSet<StudentAssignment> StudentAssignments { get; set; }
        public DbSet<ClassroomGroup> ClassroomsGroups { get; set; }
        public DbSet<SchoolClass> SchoolClasses { get; set; }
        public DbSet<GroupAssignment> GroupAssignments { get; set; }
        public DbSet<Task> Tasks { get; set; }
        public DbSet<Subject> Subjects { get; set; }

IdentityDbContext

// see https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0
    public abstract class IdentityApplicationDbContext : IdentityDbContext<
        ApplicationUser, ApplicationRole, string,
        IdentityUserClaim<string>, ApplicationUserRole, IdentityUserLogin<string>,
        IdentityRoleClaim<string>, IdentityUserToken<string>>
    {
        public IdentityApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ApplicationUser>(b =>
            {
                // Each User can have many entries in the UserRole join table
                b.HasMany(e => e.UserRoles)
                    .WithOne(e => e.User)
                    .HasForeignKey(ur => ur.UserId)
                    .IsRequired();
            });

            modelBuilder.Entity<ApplicationRole>(b =>
            {
                // Each Role can have many entries in the UserRole join table
                b.HasMany(e => e.UserRoles)
                    .WithOne(e => e.Role)
                    .HasForeignKey(ur => ur.RoleId)
                    .IsRequired();
            });
        }
    }

ApplicationUser

public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
        public virtual ICollection<ClassroomGroup> TeacherGroups { get; set; }
        public virtual ICollection<ClassroomGroup> StudentGroups { get; set; }
        public virtual ICollection<Task> CreatedTasks { get; set; }
        public virtual ICollection<SchoolClassStudentTerm> SchoolClassStudentTerms { get; set; }
        public virtual ICollection<GroupAssignment> CreatedGroupAssignments { get; set; }
        public bool Disabled { get; set; }
        public String FullName { get; set; }
        public DateTime? EspStartTime { get; set; }
    }

ClassroomGroup

public class ClassroomGroup
    {
        public int SubjectId { get; set; }
        public virtual Subject Subject { get; set; }
        public virtual SchoolClass SchoolClass { get; set; }
        public int SchoolClassId { get; set; }
        public String TeacherId { get; set; }
        public virtual ApplicationUser Teacher { get; set; }
        public int Id { get; set; }
        public String Title { get; set; }
        public virtual ICollection<ApplicationUser> Students { get; set; }
        public int TermId { get; set; }
        public virtual Term Term { get; set; }
        public virtual ICollection<GroupAssignment> GroupAssignments { get; set; }
    }
Krexci
  • 11
  • 2
  • Please show the entire `ApplicationDbContext` class. – rfmodulator Dec 06 '21 at 21:16
  • i added it to the post – Krexci Dec 07 '21 at 09:05
  • I ***think*** the problem is your custom `ApplicationUser`, you need to tell the context about it, something like: `public class ApplicationDbContext : IdentityDbContext` As it stands, the context has no knowledge of `EspStartTime` because it's using the built-in `IdentityUser` ...but I don't know what `IdentityApplicationDbContext` is, so adjust accordingly. – rfmodulator Dec 07 '21 at 17:46
  • I tried to do that but it doesnt seem like IdentityDbContext takes any arguments, I added it to the post if that helps. – Krexci Dec 07 '21 at 22:47
  • Okay, that part looks right to me, you have `IdentityApplicationDbContext : IdentityDbContext` Did you add the migration and update the database? – rfmodulator Dec 07 '21 at 23:15
  • I still cant add new migrations nor update the database, I'm still getting the same error. – Krexci Dec 08 '21 at 15:22
  • Check https://stackoverflow.com/q/49053328/1048799, there is an edit in the OP, and a link in a comment, one of these ideas may help. They are a few years old, however. – rfmodulator Dec 08 '21 at 18:07

0 Answers0