Impossible de déterminer les principaux fin de la relation de Plusieurs entités ajoutées peuvent avoir la même clé primaire

Je suis en utilisant Entity Framework 5 le Premier Code et j'ai le modèle suivant pour les constructeurs de voitures, des voitures et des camions:

public class Manufacturer
{
    public int Id { get; set; }
    public string Name { get; set; }

    [ForeignKey("ManufacturerId")]
    public virtual List<Car> Cars { get; set; }
    [ForeignKey("ManufacturerId")]
    public virtual List<Truck> 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<Manufacturer> Manufacturers { get; set; }
    public DbSet<Vehicle> Vehicles { get; set; }
}

Pour le développement et les tests je suis de semis les données comme suit:

public class DbInitialiser : DropCreateDatabaseAlways<Context>
{
    protected override void Seed(Context context)
    {
        var manufacturers = new List<Manufacturer>
        { 
            new Manufacturer
                {
                    Name = "Test Manufacturer",
                    Cars = new List<Car>
                        {
                            new Car { Colour = "Blue" },
                            new Car { Colour = "Green" }
                        },
                    Trucks = new List<Truck>
                        {
                            new Truck { Colour = "Red" }
                        }
                },
            new Manufacturer
                {
                    Name = "Another Manufacturer",
                    Cars = new List<Car>
                        {
                            new Car { Colour = "Pink" }
                        }
                }
        };

        manufacturers.ForEach(x => context.Manufacturers.Add(x));
    }
}

Cependant quand j'utilise le contexte, j'obtiens l'exception suivante:
Incapable de déterminer la principale fin de la " EF_Associations.Vehicle_Manufacturer' relation. Plusieurs entités ajoutées peuvent avoir la même clé primaire.

Trace de la pile:

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.<InitializeDatabase>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<TResult>.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.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
        at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>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: 

Comment pourrais-je aller sur la définition du principe à la fin de cette relation?

J'ai essayé d'ajouter la configuration suivante par l'intermédiaire du modèle générateur d'API fluent, mais sans succès:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Vehicle>()
        .HasRequired(v => v.Manufacturer)
        .WithRequiredDependent();
}

OriginalL'auteur Tom Hunter | 2013-09-11