Starting with Entity Framework 6.1.1, an argument exception is raised for navigation properties when using entity splitting. Interestingly, this only occurs when more than 2 tables are mapped to one entity.
```
public class Customer
{
public int CompanyNumber { get; set; }
public bool Active { get; set; }
public int PaymentTermsNumber { get; set; }
public string LabelNotes { get; set; }
public int FreightTermsNumber { get; set; }
public virtual PaymentTerms PaymentTerms { get; set; }
public virtual FreightTerms FreightTerms { get; set; }
public class EntityTypeConfiguration : EntityTypeConfiguration<Customer>
{
public EntityTypeConfiguration()
{
Property(p => p.CompanyNumber).HasColumnName("COMPANY_NO");
HasKey(k => k.CompanyNumber);
Map(m =>
{
m.Property(p => p.Active).HasColumnName("ACTIVE_FL");
m.ToTable("F_CUSTOMER", "GLOBAL");
});
// No exception will be raised for the first or second table referenced (i.e. FreightTerms)
Map(m =>
{
m.Property(p => p.LabelNotes).HasColumnName("LABEL_NOTES");
m.Property(p => p.FreightTermsNumber).HasColumnName("FREIGHT_TERM_NO");
m.ToTable("F_CUSTOMER_PREFS", "GLOBAL");
});
// System.ArgumentException: The item with identity 'Customer_PaymentTerms1' already exists in the metadata collection.
Map(m =>
{
m.Property(p => p.PaymentTermsNumber).HasColumnName("PAYMENT_TERM_NO");
m.ToTable("F_CUSTOMER_CREDIT", "GLOBAL");
});
HasRequired(p => p.FreightTerms).WithMany().HasForeignKey(p => p.FreightTermsNumber);
HasRequired(p => p.PaymentTerms).WithMany().HasForeignKey(p => p.PaymentTermsNumber);
}
}
}
```
```
System.ArgumentException was unhandled
HResult=-2147024809
Message=The item with identity 'Customer_PaymentTerms1' already exists in the metadata collection.
Parameter name: item
Source=EntityFramework
ParamName=item
StackTrace:
at System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1.AddInternal(T item)
at System.Data.Entity.Core.Metadata.Edm.MetadataCollection`1.Add(T item)
at System.Data.Entity.Core.Metadata.Edm.EntitySetBaseCollection.Add(EntitySetBase item)
at System.Data.Entity.Core.Metadata.Edm.EntityContainer.AddEntitySetBase(EntitySetBase entitySetBase)
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.AddAssociationSet(EdmModel model, AssociationSet associationSet)
at System.Data.Entity.Core.Metadata.Edm.ForeignKeyBuilder.SetOwner(EntityType owner)
at System.Data.Entity.Core.Metadata.Edm.EntityType.AddForeignKey(ForeignKeyBuilder foreignKeyBuilder)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.ForeignKeyPrimitiveOperations.CopyForeignKeyConstraint(EdmModel database, EntityType toTable, ForeignKeyBuilder fk, Func`2 selector)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.ForeignKeyPrimitiveOperations.<>c__DisplayClass39.<CopyAllForeignKeyConstraintsForColumn>b__37(ForeignKeyBuilder fk)
at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.ForeignKeyPrimitiveOperations.CopyAllForeignKeyConstraintsForColumn(EdmModel database, EntityType fromTable, EntityType toTable, EdmProperty column, EdmProperty movedColumn)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.TableOperations.CopyColumnAndAnyConstraints(EdmModel database, EntityType fromTable, EntityType toTable, EdmProperty column, Func`2 isCompatible, Boolean useExisting)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingOperations.UpdatePropertyMapping(EdmModel database, Dictionary`2 columnMappingIndex, ColumnMappingBuilder propertyMappingBuilder, EntityType fromTable, EntityType toTable, Boolean useExisting)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingOperations.MovePropertyMapping(DbDatabaseMapping databaseMapping, MappingFragment fromFragment, MappingFragment toFragment, ColumnMappingBuilder propertyMappingBuilder, Boolean requiresUpdate, Boolean useExisting)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest, EntityType entityType, EntityTypeMapping& entityTypeMapping, Boolean isMappingAnyInheritedProperty, Int32 configurationIndex, Int32 configurationCount, IDictionary`2 commonAnnotations)
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureTablesAndConditions(EntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues)
at System.Data.Entity.DbSet`1.Find(Object[] keyValues)
at IsolateCustomerEFIssue.Form1.timer1_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at IsolateCustomerEFIssue.Program.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
```
Comments: If I am not mistaken it started happening after this:
7bbd3a57f40638b7e3955e3df40fe914eff19c26 - Fix for #2103 - Add-migration fails after creating TPT schema
At first glance the SetPreferredName call seems to interfere with the name uniquification.