1

Closed

Migrating from EF5 to EF6: InversePropertyAttribute broken?

description

This has been reported on stackoverflow as a regression from EF5.
Closed Jan 21 at 6:04 PM by BriceLambson

comments

maumar wrote Nov 6, 2013 at 12:48 AM

Verified this is a regression - scenario works in EF5 and produces a correct database schema
public abstract class Record<T> where T : class
    {
        [Key]
        public long Id { get; set; }

        public long MasterId { get; set; }

        [ForeignKey("MasterId")]
        public virtual T Master { get; set; }

        [InverseProperty("Master")]
        public virtual ICollection<T> Suggestions { get; set; }
    }

    public class Concrete : Record<Concrete>
    {
        public string Foo { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Concrete> Entities { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

            using (var ctx = new MyContext())
            {
                var concrete1 = new Concrete
                {
                    Id = 1,
                    Foo = "Concrete1",
                };

                concrete1.Master = concrete1;

                var concrete11 = new Concrete
                {
                    Id = 11,
                    Master = concrete1,
                    Foo = "Concrete11",
                };

                var concrete12 = new Concrete
                {
                    Id = 12,
                    Master = concrete1,
                    Foo = "Concrete12",
                };

                var concrete111 = new Concrete
                {
                    Id = 111,
                    Master = concrete11,
                    Foo = "Concrete111",
                };

                ctx.Entities.Add(concrete1);
                ctx.Entities.Add(concrete11);
                ctx.Entities.Add(concrete12);
                ctx.Entities.Add(concrete111);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
                ctx.Entities.ToList();
            }
        }
    }

emilcicos wrote Nov 18, 2013 at 4:33 AM

Fixed in changeset 901c80f177f188e6fb32f93e9cc6409aa3bbfdcb

BriceLambson wrote Jan 20 at 7:18 PM

Fixed in changeset a0351ee0a113ec9bed03cc9cf3245942008d037f