c# - How to customize name, length and value of a discriminator column with Entity Framework 6 -
i try use ef6 existing database must compatible with. database has been generated nhibernate , tables use table-per-hierarchy (tph) inheritance model.
discriminator columns named category (instead of discriminator) , have sql length of 255 (instead of 128). values different ef generate.
how can configure ef use existing columns (name, size , values)?
i tried define custom convention:
protected class discriminatorrenamingconvention : istoremodelconvention<edmproperty> { public void apply(edmproperty property, dbmodel model) { if (property.name == "discriminator") { property.name = "category"; property.maxlength = 255; } } } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<myentitya>().map(x => x.requires("category").hasvalue("cata")); modelbuilder.entity<myentityb>().map(x => x.requires("category").hasvalue("catb")); modelbuilder.conventions.add<discriminatorrenamingconvention>(); base.onmodelcreating(modelbuilder); } this generates column named category length of 128, whatever order of instructions in onmodelcreating method. specifying category values seems overwrite maxlength.
here
modelbuilder.entity<myentitya>().map(x => x.requires("category").hasvalue("cata")); you specify name of discriminator "category", this
if (property.name == "discriminator") will evaluate false, hence maxlength ef default.
to desired behavior, use requires("discriminator") , let convention rest.
Comments
Post a Comment