简体   繁体   中英

Entity framework code first does not create many-to-many database table

I have code first model like shown below. Application creates table 'VideoPosts', and does not create 'ImagePosts'. Is there a problem with CoverImage and TileImage navigation properties, or am I missing something? I want to have a table for images just like table for videos.

public class Post
    {

        public int PostID { get; set; }

        [ForeignKey("TileImageID")]
        public Image TileImage { get; set; }
        [Column("TileImageID")]
        public int? TileImageID { get; set; }
        [ForeignKey("TileImageID")]
        public Image CoverImage { get; set; }
        [Column("CoverImageID")]
        public int? CoverImageID { get; set; }

        public virtual ICollection<Image> Gallery { get; set; }
        public virtual ICollection<Video> VideoGallery { get; set; }
    }

    public class Video
    {
        public int VideoID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        [Required]
        public string Url { get; set; }
        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Image
    {
        public int ImageID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        [Required]
        public string Path { get; set; }

        public virtual ICollection<Post> Posts { get; set; }
    }

数据库创建

One of your foreign key annotations is wrong other than that I don't see anything wrong with your DB.

    [ForeignKey("TileImageID")]
    public Image TileImage { get; set; }
    [Column("TileImageID")]
    public int? TileImageID { get; set; }
    [ForeignKey("CoverImageID")]
    public Image CoverImage { get; set; }
    [Column("CoverImageID")]
    public int? CoverImageID { get; set; }

Try in DataContext put this:

    public DataContext() : base("DatabaseName")
    {
       // Database.SetInitializer<DataContext>(new DropCreateDatabaseAlways<DataContext>());
        Database.SetInitializer<DataContext>(new CreateDatabaseIfNotExists<DataContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

Delete physically database and try run application, then application will be automatic create database and then you will see 'ImagePosts'. In your code i don't see anything wrong

Your code does't show this, but from the errors you are getting I assume that you are overriding OnModelCreating.This is where IdentityDbContext configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

So either this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         base.OnModelCreating(modelBuilder);
    }

Or you do the mapping:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

I managed to create a desired table with custom mapping. TileImage and CoverImage are also in the model, I think there was no other problems with a model.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            modelBuilder.Entity<Post>()
                .HasMany<Image>(s => s.Gallery)
                .WithMany(c => c.Posts)
                .Map(cs =>
                {
                    cs.MapLeftKey("PostID");
                    cs.MapRightKey("ImageID");
                    cs.ToTable("PostImages");
                });
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM