Reputation: 18068
My model classes look like that:
public class Car {
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
ApplicationDbContext class:
public class ApplicationDbContext : DbContext {
public DbSet<Car> Cars { get; set; }
public ApplicationDbContext()
: base("Rental") {
Configuration.LazyLoadingEnabled = true;
}
}
and the Seed method:
protected override void Seed(ApplicationDbContext context) {
context.Cars.AddOrUpdate(c => c.Id,
new Car { Id = 1, Make = "BMW", Model = "750i" },
new Car { Id = 2, Make = "Audi", Model = "A6" },
new Car { Id = 3, Make = "Honda", Model = "Civic" }
);
}
After i execute(as many times as I want) update-database
in Package Manager Console these object are once added to the database.
But after I added subclasses of Car class:
public class Car {
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public virtual ICollection<Rental> Rentals { get; set; }
}
public class JetCar : Car {
public int Thrust { get; set; }//kN
}
public class Dragster : Car {
public double Acceleration { get; set; }
}
then modified ApplicationDbContext:
public class ApplicationDbContext : DbContext {
public DbSet<Car> Cars { get; set; }
public DbSet<JetCar> JetCars { get; set; }
public DbSet<Dragster> Dragsters { get; set; }
public ApplicationDbContext()
: base("Rental") {
Configuration.LazyLoadingEnabled = true;
}
}
and then Seed method
protected override void Seed(ApplicationDbContext context) {
context.Cars.AddOrUpdate(c => c.Id,
new Car { Id = 1, Make = "BMW", Model = "750i" },
new Car { Id = 2, Make = "Audi", Model = "A6" },
new Car { Id = 3, Make = "Honda", Model = "Civic" }
);
context.Dragsters.AddOrUpdate(d => d.Id,
new Dragster { Id = 4, Make = "Chevy", Acceleration = 3.23, }
);
context.JetCars.AddOrUpdate(d => d.Id,
new JetCar { Id = 4, Make = "Jetty", Thrust = 89 }
);
}
Then after I executed update-database
I ended up with duplicates of orignal Honda, BMW and Audi cars but with discriminator set to Car
. Why is it so? How to prevent this?
Upvotes: 2
Views: 75
Reputation: 6639
You have duplicates because for Entity FrameWork, they are different. When you use inheritance, EF keeps the same table, but with a Discriminator. Note that if there is no inheritance, there is no discriminator (no need for it).
The Cars you added first are the real Car
type, but since inheritance doesn't exist (yet), EF doesn't add discriminator.
When you add Dragster
and JetCar
, EF now needs Discriminator.
Since your first added cars don't have discriminator, they are not recognized by EF. Now, EF is looking for Discriminator = 'Car' when querying the Car
type.
You can either change your seed script and adjust some data manually (you can add manuel query in the migration that add the Dragster
and JetCar
)
or you should swap inheritance for a link between 2 tables.
Upvotes: 1
Reputation: 65860
You have to test as shown below for all your seeds objects.
var car1 = context.Cars.FirstOrDefault(c => c.Make == " BMW ");
if(car1 == null)
{
context.Cars.Add(new Car { Id = 1, Make = "BMW", Model = "750i" });
}
Upvotes: 0