This project is read-only.


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


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

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


divega wrote Mar 24, 2014 at 11: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())

    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 25, 2014 at 12:43 AM

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, 2014 at 8:19 AM

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, 2014 at 4: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.

        // 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.

        // Add the property back to Members.

emilcicos wrote Mar 27, 2014 at 11:46 PM

commit e048a9418ee3ee5c49e337da511bbb283d22c34c
master branch