Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to determine the principal end of the relationship - Multiple added entities may have the same primary key
    text
    copied!<p>I'm using Entity Framework 5 Code First and I have the following model for car manufacturers, cars, and trucks:</p> <pre class="lang-cs prettyprint-override"><code>public class Manufacturer { public int Id { get; set; } public string Name { get; set; } [ForeignKey("ManufacturerId")] public virtual List&lt;Car&gt; Cars { get; set; } [ForeignKey("ManufacturerId")] public virtual List&lt;Truck&gt; Trucks { get; set; } } public class Vehicle { public int Id { get; set; } public string Colour { get; set; } public int ManufacturerId { get; set; } public virtual Manufacturer Manufacturer { get; set; } } public class Car : Vehicle { } public class Truck : Vehicle { } public class Context : DbContext { public DbSet&lt;Manufacturer&gt; Manufacturers { get; set; } public DbSet&lt;Vehicle&gt; Vehicles { get; set; } } </code></pre> <p>For development and testing I'm seeding the data as follows:</p> <pre class="lang-cs prettyprint-override"><code>public class DbInitialiser : DropCreateDatabaseAlways&lt;Context&gt; { protected override void Seed(Context context) { var manufacturers = new List&lt;Manufacturer&gt; { new Manufacturer { Name = "Test Manufacturer", Cars = new List&lt;Car&gt; { new Car { Colour = "Blue" }, new Car { Colour = "Green" } }, Trucks = new List&lt;Truck&gt; { new Truck { Colour = "Red" } } }, new Manufacturer { Name = "Another Manufacturer", Cars = new List&lt;Car&gt; { new Car { Colour = "Pink" } } } }; manufacturers.ForEach(x =&gt; context.Manufacturers.Add(x)); } } </code></pre> <p>However when I use the context I get the following exception: <strong>Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key.</strong></p> <p>Stack trace:</p> <pre><code>System.Data.DataException was unhandled HResult=-2146233087 Message=An exception occurred while initializing the database. See the InnerException for details. Source=EntityFramework StackTrace: at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.&lt;InitializeDatabase&gt;b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() 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.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator() at EF_Associations.Program.Main(String[] args) in c:\temp\EF-Associations\Program.cs:line 19 ... InnerException: System.Data.Entity.Infrastructure.DbUpdateException HResult=-2146233087 Message=Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key. Source=EntityFramework StackTrace: at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at System.Data.Entity.DropCreateDatabaseAlways`1.InitializeDatabase(TContext context) at System.Data.Entity.Database.&lt;&gt;c__DisplayClass2`1.&lt;SetInitializerInternal&gt;b__0(DbContext c) at System.Data.Entity.Internal.InternalContext.&lt;&gt;c__DisplayClass8.&lt;PerformDatabaseInitialization&gt;b__6() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) InnerException: System.Data.UpdateException HResult=-2146233087 Message=Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key. Source=System.Data.Entity StackTrace: at System.Data.Mapping.Update.Internal.UpdateTranslator.RegisterEntityReferentialConstraints(IEntityStateEntry stateEntry, Boolean currentValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.RegisterReferentialConstraints(IEntityStateEntry stateEntry) at System.Data.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager() at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands() at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() InnerException: </code></pre> <p>How would I go about specifying the principle end of this relationship?</p> <p>I've tried adding the following configuration via the model builder fluent API but without success:</p> <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;Vehicle&gt;() .HasRequired(v =&gt; v.Manufacturer) .WithRequiredDependent(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload