4

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

4 Answers 4

1

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");
                });
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I corrected annotation now. Why dont I get a new table called 'ImagePosts', just like 'VideoPosts' (see picture). I want to create many-to-many relationship with Post and Image. One post has many images (Gallery), and one Image can be used in many Posts.
Is your database up to date i.e. on the latest migration ?
Yes, it is up to date, but currently I am not using migrations, I delete database (physically) and detach it, so the database is created from scratch every time I run the app.
0

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

5 Comments

Remove comment from first row and put on second row
Now I get those errors: MyApp.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. MyApp.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
base.OnModelCreating(modelBuilder); must be called. But I still do not have table I want. I think the problem is not in database recreation, maybe the class model is somehow wrong because of TileImage and CoverImage navigation properties.
I will write you in second comment
After I commented TileImage and CoverImage fields, a new table 'PostImages' was created as desired, so those fields are the cause of the problem. How can I accomplish to have references to Image by those two fields and to have many to many table for image gallery?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.