0

I am new in Asp.net MVC core 2.0 version I created my models with 1: M relation and M: M relationship but getting error when trying to insert anything.

Error InvalidOperationException: Unable to determine the relationship represented by navigation property 'Album.Categories' of type 'ICollection'. Either manually configure the relationship, or ignore this property from the model. Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)

ApplicationUser.cs file

   public class ApplicationUser : IdentityUser
        {

            public virtual ICollection<PlayList> PlayList { get; set; }
            public string IP { get; set; }
            public string Counry { get; set; }
            public bool IsUserLock { get; set; }
            public string GoogleUser { get; set; }
        }


        public class Album
        {

            public int Id { get; set; }
            [Required]
            public string Name { get; set; }
            public string About { get; set; }
            public string Folder { get; set; }
            public bool Approve { get; set; }

            public string Picture { get; set; }
            public System.DateTime CreateDate { get; set; }
            public virtual ICollection<AudioSong> AudioSongs { get; set; }
            public virtual ICollection<Category> Categories { get; set; }
     public virtual ICollection<Album_Comments> Album_Comments { get; set; }
            public virtual ICollection<Tag> Tags { get; set; }
            public bool IsHomePage { get; set; }
            public bool Featured { get; set; }
            public bool EditorsPick { get; set; }
            public bool GaanaSpecials { get; set; }


        }

        public class Category
        {

            public int Id { get; set; }
           [Required]
            public string Name { get; set; }
            public bool Featured { get; set; }
            public System.DateTime CreateDate { get; set; }
            public virtual ICollection<Album> Album { get; set; }
            public virtual ICollection<AudioSong> AudioSong { get; set; }
        public virtual ICollection<Video_Album> Video_Album { get; set; }
        }
        public class AudioSong
        {

            public int Id { get; set; }
          [Required]
            public string Name { get; set; }
            public string Url { get; set; }
            public string Lyrics { get; set; }
            public string Singer1 { get; set; }
            public string Singer2 { get; set; }
            public string Top10 { get; set; }
            public string Top10no { get; set; }
            public string Picture { get; set; }
            public virtual Album Albums { get; set; }
            public System.DateTime CreateDate { get; set; }

   public virtual ICollection<Album_Comments> Album_comments { get; set; }
            public virtual ICollection<Actor> Actors { get; set; }
            public virtual ICollection<Category> Category { get; set; }
            public virtual ICollection<Tag> Tag { get; set; }
            public virtual ICollection<PlayList> PlayList { get; set; }

            public bool IsHomePage { get; set; }
            public bool Treading { get; set; }
            public bool IsSlider { get; set; }
        }

**ApplicationDbContext.CS**

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Actor> Actor { get; set; }

        public DbSet<Album> Album { get; set; }
        //     public DbSet<Video_Song> Video_Song { get; set; }
        public DbSet<Category> Category { get; set; }
        public DbSet<AudioSong> AudioSong { get; set; }
        //public DbSet<Video_Song_Album> Video_Song_Album { get; set; }
        public DbSet<Album_Comments> Album_Comments { get; set; }
        //public DbSet<Video_Album_Comments> Video_Album_Comments { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Langauge> Langauge { get; set; }
        public DbSet<Lyric_writer> Lyric_writer { get; set; }
        public DbSet<Publisher> Publisher { get; set; }
        public DbSet<Singer> Singer { get; set; }
        public DbSet<Site> Site { get; set; }
        //   public DbSet<Song_Comments> Song_comments { get; set; }
        // public DbSet<Video_Song_Comments> Video_Song_Comments { get; set; }
        public DbSet<PlayList> PlayList { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
           //   public virtual ICollection<PlayList> PlayList { get; set; }

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {



            base.OnModelCreating(builder);

            // Change the name of the table to be Users instead of AspNetUsers
            builder.Entity<IdentityUser>()
            .ToTable("Users");
            builder.Entity<ApplicationUser>()
            .ToTable("Users");



            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);



    }





        //   public System.Data.Entity.DbSet<SindhiColor.Models.ApplicationUser> IdentityUsers { get; set; }

        //   object placeHolderVariable;
        // public System.Data.Entity.DbSet<SindhiColor.Models.ApplicationUser> ApplicationUsers { get; set; }



    }
}

Appsettings

{
  "ConnectionStrings": {


    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-SindhiMusic-0EA272D7-78F8-4106-A564-0482CB89E7C8;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

1 Answer 1

1

EF Core does not currently support many to many relationships. You need to use an explicit entity to join the two sides, such that you have a one to many from each side to that. For example:

public class AlbumCategory
{
    [Key, Column(Order = 1)]
    [ForeignKey(nameof(Album))]
    public int AlbumId { get; set; }
    public Album Album { get; set; }

    [Key, Column(Order = 2)]
    [ForeignKey(nameof(Category))]
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Album
{
    ...

    public ICollection<AlbumCategory> Categories { get; set; }
}

public class Category
{
    ...

    public ICollection<AlbumCategory> Albums { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.