Answer is the Parent table and AnswerDetail is the child. Below works, but I'm wondering if there is a better way to do this using EF?
My Method Signature is this:
[HttpPost]
public ActionResult Evaluator(EvaluationVM evaluation, string command)
It is coming from a MVC 4 application.
Answer AnswerRecord;
AnswerRecord = (db.Answers).Where(x => x.TeacherID.Equals(evaluation.CurrentTeacher.ID)).Where(y => y.LeaderID != null).FirstOrDefault<Answer>();
if (AnswerRecord == null)
{
//Add New Parent Record:
AnswerRecord.CreatedBy = userName;
AnswerRecord.CreateStamp = DateTime.Now;
db.Entry(AnswerRecord).State = EntityState.Added;
AnswerRecord.UpdatedBy = userName;
AnswerRecord.UpdateStamp = DateTime.Now;
}
AnswerRecord.UpdatedBy = userName;
AnswerRecord.UpdateStamp = DateTime.Now;
//I use this block if I know the child record is new:
adItem = db.AnswerDetails.Find(item.LeaderAnswerDetailKey);
if (adItem != null)
{
adItem.Comment = item.LeaderComment;
adItem.AnswerOptionKey = item.LeaderAnswerOptionKey.Value;
adItem.UpdatedBy = userName;
adItem.UpdateStamp = DateTime.Now;
db.Entry(adItem).State = EntityState.Modified;
}
foreach (EvaluationObject item in evaluation.ResultSet)
{
//I use this block if I am updated the child record
adItem = new AnswerDetail();
adItem.QuestionID = item.QuestionID.Value;
adItem.AnswerOptionKey = item.TeacherAnswerOptionKey.Value;
adItem.Comment = item.TeacherComment;
adItem.CreatedBy = userName;
adItem.CreateStamp = DateTime.Now;
adItem.UpdatedBy = userName;
adItem.UpdateStamp = DateTime.Now;
adItem.AnswerKey = AnswerRecord.AnswerKey;
AnswerRecord.AnswerDetails.Add(adItem);
db.Entry(adItem).State = EntityState.Added;
//This is called at the end of my method:
db.SaveChanges();
}//End of ForEach Loop
EvaluationVM
public class EvaluationVM
{
public bool IsAdmin { get; set; }
public bool IsLeader { get; set; }
public int TeacherStatus { get; set; }
public int LeaderStatus {get;set;}
public bool IsPublicTeacher { get; set; }
public bool IsFinalTeacher { get; set; }
public bool IsPublicLeader { get; set; }
public bool IsFinalLeader { get; set; }
public List<EvaluationObject> ResultSet {get; set;}
public TeacherInfo CurrentTeacher {get; set;}
public LeaderInfo CurrentLeader { get; set; }
public List<EvaluationRating> RatingSet {get;set;}
}
EvaluationObject:
public class EvaluationObject : IEvaluationObject
{
public int? QuestionID { get; set; }
public string IndicatorID { get; set; }
public string QuestionDescription { get; set; }
public string TeacherID { get; set; }
public int? TeacherStatusKey { get; set; }
public int? TeacherAnswerKey { get; set; }
public int? TeacherAnswerDetailKey { get; set; }
public int? TeacherAnswerOptionKey { get; set; }
public string TeacherComment { get; set; }
public string LeaderID { get; set; }
public int? LeaderStatusKey { get; set; }
public int? LeaderAnswerKey { get; set; }
public int? LeaderAnswerDetailKey { get; set; }
public int? LeaderAnswerOptionKey { get; set; }
public string LeaderComment { get; set; }
}
userName
,evaluation
anditem
come from and what type they are - having the method's signature would help connect the dots ;) \$\endgroup\$