MVC4 EF5
I have 2 tables Steps and Tasks
public class Step {
public int StepID { get; set; }
public string StepName { get; set; }
public int Stepseq { get; set; }
public int BuilderID { get; set; }
public virtual Builder Builder { get; set; }
}
public class Task {
public int TaskID { get; set; }
public string StepName { get; set; }
public int Stepseq { get; set; }
public DateTime? SStart { get; set; }
public int BuilderID { get; set; }
public virtual Builder Builder { get; set; }
}
Step contains about 100 rows. I need to read the rows from step, and append (insert, add) 100 new rows to the Task table copying the StepName and StepSeq values into the appended rows. I have a StepTask Model that includes both collections:
public class StepTask {
public virtual ICollection<Step> Steps { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
Now some help please. I think this will load the 100 steps
var mycontext = new HouseContext // contains DbSet for step, task and steptask
{ var AllSteps = mycontext;
.Step
// now loop through the 100 and assign values
foreach (var mystep in AllSteps)
{
mycontext.Task.StepName = mystep.StepName
mycontext.Task.StepSeq = mystep.StepSeq
mycontext.Task.BuilderID = mystep.BuilderID
// and save each row OR move outside foreach loop and save when finished.
mxcontext.SaveChanges()
}
// done ?
}
As I work with this in VS2012 Intellisence does not show the StepName in mycontext.Tasks.StepName. It does see mycontext.Tasks and .Steps. This tells me something is not declared properly.
Should the .SaveChanges be within the loop [for each row] or after all 100 are added?
Is the BuilderID properly handled?
Do I have a problem with mycontext.Tasks vs mycontext.Task ?
Thanks