0

私はAsp.net MVCコア2.0の新バージョンです 1:MリレーションシップとM:Mリレーションシップでモデルを作成しましたが、何かを挿入しようとするとエラーが発生します。接続文字列の問題.netコアとDB作成中にエラーが発生しました

エラー InvalidOperationException: 'ICollection'タイプのナビゲーションプロパティ 'Album.Categories'によって表される関係を特定できません。リレーションシップを手動で構成するか、モデルからこのプロパティを無視します。 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilderのModelBuilder)

ApplicationUser.csをファイル

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

EFコアは現在ありません多対多の関係をサポートします。明示的なエンティティを使用して両側を結合する必要があります。つまり、各側から1対多に1対多になるようにする必要があります。例:

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; } 
} 
関連する問題