c# - Entity framework 4.0 strange with saving data -
i faced next problem. have data warehouse model
public class gameresult { public int gameresultid { get; set; } public virtual competition competition { get; set; } public virtual customdate dategame { get; set; } public virtual contender contenderfirst { get; set; } public virtual contender contendersecond { get; set; } public virtual location location { get; set; } } public class competition { [key] public int competitionid { get; set; } public string name { get; set; } //lazy loading public virtual kindsport kindsport { get; set; } }
something generate data fact table gameresult
gameresult.location = location; gameresult.competition = competition; gameresult.contenderfirst = firstcontender; gameresult.contendersecond = secondcontender; public void savegameresult(gameresult gameresult) { using (var db = new gamecontext()) { db.gameresults.add(gameresult); db.savechanges(); } }
but when try save data don't save enity in fact table getting cascade saved in child tables location, contender
.
how can solve problem?
add
marks whole object graph added
.
this way fix it:
public void savegameresult(gameresult gameresult, .... more arguments ....) { using (var db = new gamecontext()) { db.gameresults.add(gameresult); gameresult.location = location; gameresult.competition = competition; gameresult.contenderfirst = firstcontender; gameresult.contendersecond = secondcontender; db.savechanges(); } }
but have add more arguments.
a more tedious way leave way , mark each dimension unchanged
:
db.gameresults.add(gameresult); entry(gameresult.location).state = entitystate.unchanged; ...
yet way set id
values only:
gameresult.locationid = location.locationid; ... etc. public void savegameresult(gameresult gameresult) { ... (original code)
but of course primitive id properties should part of fact class, forming foreign key assocations.
Comments
Post a Comment