0

In a code-first, EF6 database with automation migrations enabled, I find that I have 4 classes: Formula, FormulaStep, Preparation, PreparationStep, but only 3 tables for them: Formulae, FormulaSteps, Preparations.

I'm very unhappy with this because, IMHO, PreparationSteps should be quite different to FormulaSteps. Realizing that this is a working application, is there an easy way to change this? To force the creation of a PreparationSteps table, with each row having a reference to the FormulaSteps row which serves as its template?

I obviously do not want to delete any data (in the db) nor break the application, which may rely on this interitance in some zany way, apart from using it to construct the database.

PS 1: A Formula here is a plan for a real-world thing. A Preparation is an instantiation of that plan. It represents that real-world thing.

PS 2: The class headers are:

[DataContract]
public class FormulaStep : BaseData, ISearchable, ICloneable
{ ... }

[DataContract]
public class PreparationStep : FormulaStep, ISearchable, ICloneable
{ ... }

1 Answer 1

0

You probably want to set up Table per Concrete Type mapping.

You can achieve this by telling the ModelBuilder into which tables it should store your inherited entities.

ModelBuilder.Entity<FormulaStep>().ToTable("FormulaSteps");
ModelBuilder.Entity<PreparationStep>().ToTable("PreparationSteps");

Then add a manual migration, it should scaffold the new table "PreparationSteps" automatically. Put additional SQL into this migration to move all rows with the discriminator value "PreparationStep" to the new table. Maybe the scaffolded migration will drop some columns from "FormulaSteps" that are no longer needed, so make sure to move the data before they are dropped (i.e. before any DropColumn statements in the migration).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.