Asp.net 连接字符串问题.net core,创建DB时出错
我是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文件Asp.net 连接字符串问题.net core,创建DB时出错,asp.net,asp.net-core,asp.net-core-mvc,connection-string,Asp.net,Asp.net Core,Asp.net Core Mvc,Connection String,我是Asp.net MVC core 2.0版本的新手 我用1:M关系和M:M关系创建了模型,但在尝试插入任何内容时出错 错误 InvalidOperationException:无法确定“ICollection”类型的导航属性“Album.Categories”表示的关系。手动配置关系,或者从模型中忽略此属性。 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvent
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; }
}
}
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; }
}
公共类相册类别
{
[键,列(顺序=1)]
[外键(专辑名称))]
public int AlbumId{get;set;}
公共相册相册{get;set;}
[键,列(顺序=2)]
[外键(名称(类别))]
public int CategoryId{get;set;}
公共类别{get;set;}
}
公开课相册
{
...
公共ICollection类别{get;set;}
}
公共类类别
{
...
公共ICollection相册{get;set;}
}
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; }
}