3

I'm working on a university management web app that I'm trying to make for a school project. Currently I have 3 classes in my project as follows:

public abstract class User : IdentityUser<string>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string DepartmentId { get; set; }
        public Department Department { get; set; }

        public List<Course> Courses { get; set; }
        public List<Community> Communities { get; set; }

        public List<Event> Events { get; set; }
    }
public class Student : User
    {
        public string RollNumber { get; set; }     
    }
public class Instructor : User
    {     
    }

The DbContext is as follows:

public class AppDbContext : IdentityDbContext<User, Role, string>
    {

        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }


        //public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        //{
        //    return base.SaveChangesAsync(cancellationToken);
        //}

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

        }


        protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
        {
            base.ConfigureConventions(configurationBuilder);
        }


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


         
            builder.ApplyConfiguration(new UserConfig());
            builder.ApplyConfiguration(new BaseEntityConfig());
            builder.ApplyConfiguration(new CommunityConfig());
            builder.ApplyConfiguration(new CourseConfig());
            builder.ApplyConfiguration(new DepartmentConfig());
            builder.ApplyConfiguration(new EventConfig());
            builder.ApplyConfiguration(new RoomConfig());
            builder.ApplyConfiguration(new StudentConfig());
            builder.ApplyConfiguration(new RoleConfig());



        }



        public DbSet<Community> Communities { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<Instructor> Instructors { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Event> Events { get; set; }

    }

And this is the configuration for abstract class User:

public class UserConfig : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {

            builder.Property(u => u.Id).HasValueGenerator<UserIdGenerator>();
            builder.UseTpcMappingStrategy();
        }



    }

What I want to do is have students and teachers in separate tables and inherit properties from User class. The reason why I made the User class abstract is to prevent the creation of objects by saying "new User()" in the program. When configuring the User class, i get the error "The corresponding CLR type for entity type 'User' cannot be instantiated, but the entity type was mapped to 'AspNetUsers' using the 'TPC' mapping strategy. Only instantiable types should be mapped." What I understand from this error is that when I use the IdentityDbContext, it wants a non-abstract user class for the AspNetUsers table. But I'm trying to make it abstract because I don't want User = new User() to be made in the application. How can i solve this issue?

  • You can workaround that limitation with layers and only exposing classes derived from `User` to the outer layers. – iamdlm Mar 14 '23 at 14:22
  • Based on your scenario, you ought to consider using the [`Factory Pattern`](https://refactoring.guru/design-patterns/factory-method). You made the constructor private, or use a static method to return an instance rather creating new. You could [`have look here for more details`](https://learn.microsoft.com/en-us/archive/msdn-magazine/2003/march/design-patterns-create-dynamic-factories-in-net-with-reflection) – Md Farid Uddin Kiron Mar 15 '23 at 08:30

0 Answers0