连接字符串问题创建数据库时出现.net核心和获取错误

时间:2017-10-02 02:29:20

标签: asp.net asp.net-core asp.net-core-mvc connection-string

我是Asp.net MVC核心2.0版本的新手 我创建了具有1:M关系和M:M关系的模型,但在尝试插入任何内容时出现错误。

错误 InvalidOperationException:无法确定导航属性' Album.Categories'表示的关系。类型' ICollection'。手动配置关系,或从模型中忽略此属性。 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 个答案:

答案 0 :(得分:1)

EF Core目前不支持多对多关系。您需要使用显式实体来加入双方,这样您从每一方都有一对多。例如:

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; }
}