Not sure if im mis-understanding how EF Code first works or have set it up wrong. I have created a POCO of Charts which has many ChartResults. Simple one to many.
When I do
Chart myChart = new Chart();
myChart.ChartResults.Add(new ChartResult(){Pos=1});
myChart.ChartResults.Add(new ChartResult(){Pos=2});
context.Charts.Add(myChart);
context.Save();
I get a chart added, and then it tears through all of the chart results and adds that to the database too. This is very clever im sure but the problem is on the chartResults sometimes I want to add and sometimes I want to simply update the reference. But how do I achieve this without:
1) going through each Chart and adding it to a new object that is unlinked
2) then going through each ChartResult and explicitely deciding what to do/not do?
if you know which
chartresultsto add and which to update then you can load thechartand add all the newchartresultinstances. then you can query for the existingchartresultsand update them individually.Or you can loop through the
chartresultsand determine if the entity needs to be added or updated.BTW: this is irrelevant if EF was mapped via EDMX or code first. this is simply how you interact with the entities, not how to map the entities.