1

Closed

Regression: After upgrade 6.0.2 -> 6.1.0 specifying column name different from default fails if entity key has 10 or more member properties

description

I have following configuration to map existing DB:
this.Property(x => x.PafAddress1).HasColumnName("PAF_Address1").IsOptional().HasMaxLength(511);
After upgrade to EF 6.1.0

I have now error:

The item with identity 'PAF_Address1' already exists in the metadata collection.
Parameter name: item


How to solve that issue and map such existing column to the model in new 6.1.0

Regards
Marcin
Closed Apr 12 at 12:10 AM by jemartti
Fix is good. Note that SQL Server has a maximum of 16 columns for primary keys.

comments

divega wrote Mar 24 at 10:01 PM

The code below repros the issue.

@msmolka: could you confirm if in your model you also have 10 or more key members?
using System.Data.Entity;

namespace BigKeyRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new VagueContext())
            {
                db.Database.Delete();
                db.Database.Initialize(false);
            }
        }
    }

    public class VagueItem
    {
        public int KeyProperty1 { get; set; }
        public int KeyProperty2 { get; set; }
        public int KeyProperty3 { get; set; }
        public int KeyProperty4 { get; set; }
        public int KeyProperty5 { get; set; }
        public int KeyProperty6 { get; set; }
        public int KeyProperty7 { get; set; }
        public int KeyProperty8 { get; set; }
        public int KeyProperty9 { get; set; }
        public int KeyProperty10 { get; set; }
        public int NonKeyProperty1 { get; set; }


    }

    public class VagueContext : DbContext
    {
        public DbSet<VagueItem> VagueItems { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var keyOrder = 0;
            // just setting up the key members
            modelBuilder.Properties().Where(info => info.Name.StartsWith("KeyProperty")).Configure(config => config.IsKey().HasColumnOrder(keyOrder++));
            // failure only occurs only if a non-default column name is specified for any property and there are 10 or more properties that are part of the key
            modelBuilder.Entity<VagueItem>().Property(x => x.NonKeyProperty1).HasColumnName("Custom");

        }
    }
}

divega wrote Mar 24 at 11:43 PM

I assigned this to Emil and updated title to reflect that the repro is more general, i.e. this not only applies to column names with underscores but to any name that is different from the default, and (pending confirmation from the customer) it seems to only apply if the entity's key has more than 10 member properties.

msmolka wrote Mar 25 at 7:19 AM

Hello
Yes, I can confirm that this happen when I have hasKey configuration with 10 properties. It has exactly 10 in my case.

emilcicos wrote Mar 25 at 3:26 PM

Unfortunately this is a bug that we missed in the functionality that updates an internal dictionary used to find a member of an entity by name. If an entity has more than 8 key properties and an attempt is made to rename a non-key property the error above will occur. 8 is the "magical" number that we use to switch from list lookup to dictionary lookup.

For the case above a possible workaround is having a convention like below:
public class MyConvention : IConceptualModelConvention<EntityType>
{
    public void Apply(EntityType entityType, DbModel model)
    {
        var property = entityType.Properties.GetValue("NonKeyProperty1", ignoreCase: false);

        // Add property to KeyMembers to allow the rename to succeed.
        entityType.AddKeyMember(property);

        // Rename
        property.Name = "Custom";

        // Remove the property from both Members and KeyMembers.
        // Removing from KeyMembers would be sufficient but we don't expose a RemoveKeyMember method.
        entityType.RemoveMember(property);

        // Add the property back to Members.
        entityType.AddMember(property);
    }
}

emilcicos wrote Mar 27 at 10:46 PM

commit e048a9418ee3ee5c49e337da511bbb283d22c34c
master branch