none
Add-Migration results in missing fields RRS feed

  • Question

  • This is for EF Core 1.0.0 RC2 using EF Core Tools 1.0.0 Preview1 final

    I have this set up:

        public class Blog
        {
            public int BlogId { get; set; }
            public string Url { get; set; }
    
            public BlogImage HeaderImage { get; set; }
            public BlogImage FooterImage { get; set; }
        }
    
        public class BlogImage
        {
            public int BlogImageId { get; set; }
            public byte[] Image { get; set; }
            public string Caption { get; set; }
    
            public int BlogId { get; set; }
            public Blog Blog { get; set; }
        }
    
    
    class MyContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }
            public DbSet<BlogImage> BlogImages { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Blog>()
                    .HasOne(p => p.HeaderImage)
                    .WithOne(i => i.Blog)
                    .HasForeignKey<BlogImage>(b => b.BlogId);
    
                modelBuilder.Entity<Blog>()
                    .HasOne(p => p.FooterImage)
                    .WithOne(i => i.Blog)
                    .HasForeignKey<BlogImage>(b => b.BlogId);
            }
        }

    Starting from a clean slate (no existing database, no existing migration), I run this command:  Add-Migration Initial-DB

    The generated code completely missed my FooterImage field in the Blogs table, including all related constraints and indexes related to this field.  The HeaderImage is ok though.  Is there a bug with Add-Migration when there is a 1-to-1 relationship defined multiple times?

    Wednesday, June 22, 2016 6:49 PM

Answers

  • Hi Typhoon Larry,

    >>Is there a bug with Add-Migration when there is a 1-to-1 relationship defined multiple times?

    Do it throw any exception?

    In addition, please try to the following code.

    public class Blog
        {
            public int BlogId { get; set; }
            public string Url { get; set; }
    
            public BlogImage HeaderImage { get; set; }
            public BlogImage FooterImage { get; set; }
        }
    
        public class BlogImage
        {
            public int BlogImageId { get; set; }
            public byte[] Image { get; set; }
            public string Caption { get; set; }
    
            public int BlogId { get; set; }
            public Blog Blog { get; set; }
    
            public int FBlogId { get; set; }
            public Blog FBlog { get; set; }
    
        }
    
    
    class MyContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }
            public DbSet<BlogImage> BlogImages { get; set; }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Blog>()
                    .HasOne(p => p.HeaderImage)
                    .WithOne(i => i.Blog)
                    .HasForeignKey<BlogImage>(b => b.BlogId);
    
                modelBuilder.Entity<Blog>()
                    .HasOne(p => p.FooterImage)
                    .WithOne(i => i.FBlog)
                    .HasForeignKey<BlogImage>(b => b.FBlogId);
            }
        }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 24, 2016 9:39 AM
    Moderator