vpetrovic
vpetrovic

Reputation: 527

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

Database created

Upvotes: 4

Views: 1593

Answers (4)

vpetrovic
vpetrovic

Reputation: 527

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

Upvotes: 1

GalaxMan
GalaxMan

Reputation: 58

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

Upvotes: 0

GalaxMan
GalaxMan

Reputation: 58

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

Upvotes: 0

James Dev
James Dev

Reputation: 3009

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

Upvotes: 0

Related Questions