c# - How to configure multiple object sets per type in Entity Framework code first -
c# - How to configure multiple object sets per type in Entity Framework code first -
i using entity framework 5 code first
. in database have 2 tables, availpayperiods
, availpayperiodsweekly
. both same:
period datetime not null
because these 2 tables identical in nature decide create next class represent either 1 of 2:
public class payperiod : ientity { public int id { get; set; } public datetime period { get; set; } }
i'm struggling configure 2. have next in database context class:
public dbset<payperiod> weeklypayperiods { get; set; } public dbset<payperiod> monthlypayperiods { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.configurations.add(new weeklypayperiodconfiguration()); // haven't yet created configuration file monthly pay periods }
my weeklypayperiodconfiguration
class:
class weeklypayperiodconfiguration : entitytypeconfiguration<payperiod> { internal weeklypayperiodconfiguration() { this.totable("availpayperiodsweekly"); } }
when phone call repository weekly pay periods next error:
multiple object sets per type not supported. object sets 'weeklypayperiods' , 'monthlypayperiods' can both contain instances of type 'epayslips.domainmodel.entities.payperiod'.
how map 2 respective tables?
should rather create separate classes called weeklypayperiod
, monthlypayperiod
?
you add together next classes:
public class monthlypayperiod : payperiod { } public class weeklypayperiod : payperiod { }
and amend dbcontext to:
public dbset<weeklypayperiod> weeklypayperiods { get; set; } public dbset<mnthlypayperiod> monthlypayperiods { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<weeklypayperiod>().map(m => { m.mapinheritedproperties(); m.totable("availpayperiodsweekly"); }); modelbuilder.entity<monthlypayperiod>().map(m => { m.mapinheritedproperties(); m.totable("availpayperiodsmonthly"); }); }
not perfect gets job done.
c# entity-framework ado.net ef-code-first entity-framework-5
Comments
Post a Comment