ICollection Errors Re: Navigation Properties

Dean Everhart 1,496 Reputation points
2023-04-07T20:31:23.1+00:00

Environment: Net Core 6, Visual Studio Community 2022 (64 bit), WIndows 11 ======== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ======== Browser Error: InvalidOperationException: The property 'Course.Enrollments' is of an interface type ('ICollection'). If it is a navigation, manually configure the relationship for this property by casting it to a mapped entity type. Otherwise, ignore the property using the [NotMapped] attribute or 'Ignore' in 'OnModelCreating'.

using System;
using System.Collections;
using System.Collections.Generic;


namespace ContosoUniverstiyEmbed.Models
{
    public class Student
    {
        public int ID { get; set; }
        public string? LastName { get; set; }
        public string? FirstMidName { get; set; }
        public DateTime? EnrollmentDate { get; set; }

        public ICollection? Enrollments { get; set; }
    }
}

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace ContosoUniverstiyEmbed.Models
{
    public class Course
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CourseID { get; set; }
        public string? Title { get; set; }
        public int? Credits { get; set; }

        public ICollection? Enrollments { get; set; }
    }
}
namespace ContosoUniverstiyEmbed.Models
{
    public enum Grade
    {
        A, B, C, D, F
    }
    public class Enrollment
    {
        public int EnrollmentID { get; set; }
        public int CourseID { get; set; }
        public int StudentID { get; set; }
        public Grade? Grade { get; set; }

        public Course? Course { get; set; }
        public Student? Student { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ContosoUniverstiyEmbed.Models;

namespace ContosoUniverstiyEmbed.Data
{
    public class ContosoUniverstiyEmbedContext : DbContext
    {
        public ContosoUniverstiyEmbedContext (DbContextOptions<ContosoUniverstiyEmbedContext> options)
            : base(options)
        {
        }

        public DbSet<ContosoUniverstiyEmbed.Models.Student> Student { get; set; } = default!;

        public DbSet<ContosoUniverstiyEmbed.Models.Course> Course { get; set; }

        public DbSet<ContosoUniverstiyEmbed.Models.Enrollment> Enrollment { get; set; }


    }
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,226 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful