ICollection Errors Re: Navigation Properties

Dean Everhart 1,536 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,553 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,291 Reputation points
    2023-04-08T11:21:52.1033333+00:00

    The collection type is missing.

    public ICollection<Enrollment>? Enrollments { get; set; }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.