【问题标题】:Connection String Issue .net core and Getting error while creating DB连接字符串问题 .net 核心和创建数据库时出现错误
【发布时间】:2017-10-02 02:29:19
【问题描述】:

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

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



    }
}

应用设置

{
  "ConnectionStrings": {


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

【问题讨论】:

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


    【解决方案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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 2015-01-03
      • 2019-09-22
      • 2017-07-13
      • 2019-09-22
      • 2023-04-08
      相关资源
      最近更新 更多