c# - EF and TPT : the column name is specified more than once in the SET clause -
i'm using ef 6 , use tpt strategy model problem. rule
abstract class. overtimerule
concrete class, inheriting rule
.
rule
looks :
public abstract class rule { public int id { get; set; } public periodtype periodtype { get; set; } public int sortorder { get; set; } public int statuteid { get; set; } public bool isactive { get; set; } }
overtimerule
looks :
public partial class overtimerule : rule { public decimal? thresholdcoefficient { get; set; } public decimal? limitcoefficient { get; set; } }
when create new overtimerule
, try save it, ef first generates query :
insert [dbo].[rules]([periodtype], [sortorder], [statuteid], [isactive], [statuteid]) values (@0, @1, @2, @3, @4, @5, @6, null)
as can see, ef adds statuteid
column insert, not exists anywhere in solution , makes invalid sql query.
sql rightfully throws : the column name 'statuteid' specified more once in set clause. column cannot assigned more 1 value in same set clause. modify set clause make sure column updated once. if set clause updates columns of view, column name 'statuteid' may appear twice in view definition.
the mapping looks :
modelbuilder.entity<rule>().totable("rules", timmischemaname); modelbuilder.entity<rule>().hasrequired(s => s.statute).withmany(s => s.rules).hasforeignkey(r => r.statuteid); modelbuilder.entity<overtimerule>().totable("overtimerules", timmischemaname);
could tell me trigger behavior ?
this 1 tricky.
modelbuilder.entity<rule>().hasrequired(s => s.statute).withmany().hasforeignkey(r => r.statuteid);
is incorrect, , should have been
modelbuilder.entity<rule>().hasrequired(s => s.statute).withmany(s => s.rules).hasforeignkey(r => r.statuteid);
as property statute.rules
existed on other side of foreign key.
without it, ef tries auto map property sql column rules table, guessed should statuteid
.
Comments
Post a Comment