Home > Software engineering >  View returns an error from an abstract type
View returns an error from an abstract type

Time:02-01

I have an abstract concert type and 3 concert classes inheriting from the base

(RegularConcert, Party, ClassicalConcert)

, they are all mapped to their own tables in the database, the thing is I can't get all of them in a single view(probably because Concert is abstract) and I get a nullReferenceException in my controller.

Models:

    public abstract class Concert //base class
    {
        [Key]
        public int ConcertId { get; set; }
        [Display(Name = "Performer name")]
        public string PerformerName { get; set; }
        [Display(Name = "Ticket ammout")]
        public int TicketsCount { get; set; }
        [Display(Name = "Date of performance")]
        public DateTime PerformanceDate { get; set; }
        [Display(Name = "Location")]
        public string Location { get; set; }
        [Display(Name = "Event discription")]
        public string Description { get; set; }
        [Display(Name = "Image source")]
        public string ImageURL { get; set; }
        public List<Ticket> Tickets { get; set; }
    }

    public class ClassicalConcert : Concert
    {
        public VoiceTypes VoiceType { get; set; } 
        public string ConcertName { get; set; }
        public string ComposersName { get; set; }

    }

    public class Party : Concert
    {
        public int AgeLimit { get; set; }
    }

    public class RegularConcert : Concert
    {

    }

Controller:

public class ConcertController : Controller
    {
        private readonly AppDbContext _context;
        public ConcertController(AppDbContext context)
        {
            _context = context;
        }
        public async Task<IActionResult> Concerts()
        {
            return View(await _context.Concerts.ToListAsync());
        }
    }

DbContext:

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);
        }
    }

Do I have to add concrete classes into DbContext as DbSet?

CodePudding user response:

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.

  • Related