Search code examples
asp.netmodel-view-controllerentity-framework-core

View returns an error from an abstract type


The Issue:

The issue was connected with the AppDbContext class, the information from the database was not being correctly sent to the database.

DbContext(with an error):

public class AppDbContext : CentaureaContext
    {
        public  DbSet<Concert> Concerts { get; set; }
        public  DbSet<Ticket> Tickets { get; set; }
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) 
        {
            
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Party>().ToTable("Parties");
            modelBuilder.Entity<RegularConcert>().ToTable("RegularConcerts");
            modelBuilder.Entity<ClassicalConcert>().ToTable("ClassicalConcerts");
            base.OnModelCreating(modelBuilder);
        }
    }

Solution:

Instead of getting only a DbSet<Concert> I needed to remove it and then to add the concrete implementations of this class:

public class AppDbContext : CentaureaContext
    {
        public DbSet<Party> Parties{get;set;}
        public DbSet<ClassicalConcert> ClassicalConcerts{get;set;}
        public DbSet<RegularConcert> RegularConcerts{get;set;}
        public  DbSet<Ticket> Tickets { get; set; }
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) 
        {
            
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Party>().ToTable("Parties");
            modelBuilder.Entity<RegularConcert>().ToTable("RegularConcerts");
            modelBuilder.Entity<ClassicalConcert>().ToTable("ClassicalConcerts");
            base.OnModelCreating(modelBuilder);
        }
    }


Solution

  • If you mean you want to get a values from model, from every classes, I should say its not true... Why? Because you should add inheritance classes (RegularConcert, Party, ClassicalConcert) to data base with dbSet and you don't need to add Concert to dbSet and create a table in database. anyway You should add another model to database and the create method to get a values from them, then call them in controller.