4

I'm using Entity Framework 5 Code First and I have the following model for car manufacturers, cars, and trucks:

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; }
}

For development and testing I'm seeding the data as follows:

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));
    }
}

However when I use the context I get the following exception: Unable to determine the principal end of the 'EF_Associations.Vehicle_Manufacturer' relationship. Multiple added entities may have the same primary key.

Stack trace:

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: 

How would I go about specifying the principle end of this relationship?

I've tried adding the following configuration via the model builder fluent API but without success:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Vehicle>()
        .HasRequired(v => v.Manufacturer)
        .WithRequiredDependent();
}
Tom Hunter
  • 5,714
  • 10
  • 51
  • 76

2 Answers2

3

You must have only one foreign key property on your Manufacturer class: Vehicles.

[ForeignKey("ManufacturerId")]
public virtual List<Vehicle> Vehicles { get; set; }

You must modify your seed method to use this single FK property. EF will create a table-per-hierarchy model inferred from the inheritance chain of your entity classes:

protected override void Seed(Context context)
{
    var manufacturers = new List<Manufacturer>
    { 
        new Manufacturer
            {
                Name = "Test Manufacturer",
                Vehicles = new List<Vehicle>
                    {
                        new Car {Colour = "Blue" },
                        new Car {Colour = "Green" },
                        new Truck {Colour = "Red" }
                    }
            },
        new Manufacturer
            {
                Name = "Another Manufacturer",
                Vehicles = new List<Vehicle>
                    {
                        new Car {Colour = "Pink" }
                    }
            }
    };

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

By convention, EF will generate an additional Discriminator column with values taken from the subclass names (Car, Truck) in your Vehicle table.

For convenience, you can create two additional (non-FK) properties on Manufacturer, each wraps an inheritance-specific OfType query:

public IEnumerable<Car> Cars
{
    get
    {
        return this.Vehicles.OfType<Car>();
    }
}

public IEnumerable<Truck> Trucks
{
    get
    {
        return this.Vehicles.OfType<Truck>();
    }
}

and use them to query:

using (var db = new Context())
{
    foreach (var manufacturer in db.Manufacturers)
    {
        var cars = manufacturer.Cars.ToList();
        var trucks = manufacturer.Trucks.ToList();
    }
}
Samu Lang
  • 2,261
  • 2
  • 16
  • 32
  • what if I have another model called "Manufacturer2" that looks exactly like Manufacturer, with a list of Vehicles? I have something similar but with 2 classes that are like the manufacturer class, and they both have a class like vehicle, and I'm getting the same exception, see my post here http://stackoverflow.com/questions/26783934/foreign-key-cycles-or-cascade-paths – Abdul Ahmad Nov 07 '14 at 00:07
0

I tested using SQL Server 2012 and 'Migrations' (http://msdn.microsoft.com/en-us/data/jj591621.aspx)

Model their classes as follows:

[Table("Manufacturer")]
public class Manufacturer
{
   public Funcionario()
   {
     Cars = new List<Car>();
     Trucks = new List<Truck>();
   }

    [Key]
    [Required]
    [Column("Id", TypeName = "INT")]
    public int Id { get; set; }

    [Column("Name", TypeName = "VARCHAR")]
    public string Name { get; set; }

    public virtual ICollection<Car> Cars  { get; set; }
    public virtual ICollection<Truck> Trucks { get; set; }
 }

 [Table("Vehicle")]
 public class Vehicle
 {
   [Key]
   [Required]
   [Column("Id", TypeName = "INT")]
   public int Id { get; set; }

   [Column("Colour", TypeName = "VARCHAR")]
   public string Colour { get; set; }

   [Column("ManufacturerId", TypeName = "INT")]
   public int ManufacturerId { get; set; }

   [ForeignKey("ManufacturerId")]
   public virtual Manufacturer Manufacturer { get; set; }
  }

 [Table("Car")]
 public class Car : Vehicle
 { }

 [Table("Truck")]
 public class Truck : Vehicle
 { }

 public class Context : DbContext
 {
    public DbSet<Manufacturer> Manufacturers { get; set; }
    public DbSet<Vehicle> Vehicles { get; set; }
    public DbSet<Car> Cars { get; set; }
    public DbSet<Truck> Trucks { get; set; }

 }
gabrielhws
  • 313
  • 3
  • 8