2

Closed

EF6 regression: identity pattern not applied to key by convention in simple inheritance scenario

description

The following code works with EF5 but fails on SaveChanges with a primary key violation with EF6. The reason seems to be that we don't make the Id column an identity in EF6 for a scenario like this:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Xml;

namespace ConsoleApplication17
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var db = new MyContext())
            {
                db.Database.Delete();
                Message message = db.Messages.Add(
                    new Message {
                       Contents = "Here", 
                       Title = "Something"});
                db.Comments.Add(
                    new Comment {
                       Contents = "I think...", 
                       Title = "My take", 
                       Parent = message});
                db.SaveChanges();
            }
        }
    }

    public abstract class MessageBase
    {
        //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Title { get; set; }
        public string Contents { get; set; }
    }

    public class Message : MessageBase
    {
    }

    public class Comment : MessageBase
    {
        public MessageBase Parent { get; set; }
        public int ParentId { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<MessageBase> MessageBases { get; set; }
        public DbSet<Message> Messages { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }
}
Closed Nov 8, 2013 at 9:52 PM by lukew
Verified that this bug no longer repros in 6.0.2 branch and 6.1.0 branch. Also, this didn't break the fix for 1101 where this regression was introduced.

comments

zunath wrote Oct 20, 2013 at 8:00 PM

Confirmed that this problem exists. Please correct it. Thank you.

divega wrote Oct 21, 2013 at 6:35 PM

Clearing up release field for re-triage. Confirmed that this issue does not get fixed with the fix for #1747. Also, using [DbModelBuilderVersion(DbModelBuilderVersion.V5_0)] doesn't help.

ajcvickers wrote Nov 4, 2013 at 5:12 PM

Fixed in changeset 4fe2a62d8a3caa77b0cf6d8ab2c1cb606a57acbf