0

I am making a web app similar to google classroom in that you can join classes. I have a class "Account" and inside that account I have a list that should hold the IDs of all the classes the account has joined. I tried to make the list a list of longs, but I couldn't do that because I got the error:

System.InvalidOperationException: 'The property 'Account._classesJoined' could not be mapped, because it is of type 'List' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

The way I solved this problem is to create a class "JoinedClassId" to make a list of instead, with a property "classIdNumber". However, during testing, I noticed that the JoinedClassIds that I added to the the Account object were not saving. I think this is because I am not saving the database table for the JoinedClassId class.

Do I have to create a database context and controller for the JoinedClassId class? I don't want to be able to manipulate the JoinedClassId class from the API, I'm only using it as a data container. Is there a way I could either create a long list and save it or save the JoinedClassIds?

atiyar
  • 7,762
  • 6
  • 34
  • 75
Andrew C
  • 137
  • 2
  • 10
  • you have many-to-many relationship, account can join multiple classes and classes can have multiple accounts joined. try implement entity that would combine data about account joining single class -> AccountClass { AccountId, ClassId } – Yehor Androsov Oct 08 '20 at 20:03
  • Does this answer your question? [Fluent API, many-to-many in Entity Framework Core](https://stackoverflow.com/questions/46184678/fluent-api-many-to-many-in-entity-framework-core) – Yehor Androsov Oct 08 '20 at 20:03

1 Answers1

0

In EF Core "Many-to-many relationships without an entity class to represent the join table are not yet supported". Book -> Category has many-to-may rel so this should create the 3 tables in DB : Books, Category and BookCategory

 public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        //public ICollection<Category> Categories { get; set; } // cannot appear 

        // For the many-to-many rel
        public List<BookCategory> BookCategories { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        //public ICollection<Book> Books { get; set; } // cannot appear 

        // For the many-to-many rel
        public List<BookCategory> BookCategories { get; set; }
    }

    // Class because of the many-to-many rel

    public class BookCategory
    {
        public int BookId { get; set; }
        public Book Book { get; set; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }

    public class MyContextDbContext : DbContext
    {
        public MyContextDbContext(DbContextOptions<MyContextDbContext> dbContextOptions)
            : base(dbContextOptions)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BookCategory>()
                .HasKey(t => new { t.BookId, t.CategoryId });

            modelBuilder.Entity<BookCategory>()
                .HasOne(bctg => bctg.Book)
                .WithMany(ctg => ctg.BookCategories)
                .HasForeignKey(book => book.CategoryId);

            modelBuilder.Entity<BookCategory>()
                .HasOne(bctg => bctg.Category)
                .WithMany(ctg => ctg.BookCategories)
                .HasForeignKey(ctg => ctg.BookId);

        }

        public DbSet<Book> Book { get; set; }
        public DbSet<Category> Category { get; set; }
    }
John777
  • 126
  • 1
  • 4