views:

37

answers:

1

Hi Guys,

I am having a real issue with the EF v1. I have quite a big EDMX with maybe 50 entitys mapped, but this one entity is causing me grief. The entity has mappings to other entitys with in effect are reference tables, but for some reason it is trying to do an Insert and not just update its self. Here is a fragment of my code..

using (var context = new someEntities()) { var studentCourseJoin = context.StudentCourseJoinSet.Where(o => o.Code == scjCode).First();

studentCourseJoin.EntryStatus = new EntryStatus { Code = viewModel.StudentDetails.EntryStatusCode };
studentCourseJoin.ParentalInHigherEducation = new ParentalInHigherEducation { Code = viewModel.StudentDetails.ParentalInHigherEducationCode };
studentCourseJoin.School = new School { Code = viewModel.StudentDetails.SchoolCode };

studentCourseJoin.Institution = new Institution { Code = viewModel.StudentDetails.InstitutionCode };

studentCourseJoin.LastSchoolEndYear = viewModel.StudentDetails.LastSchoolEndYear;
studentCourseJoin.LastInstitutionEndYear = viewModel.StudentDetails.LastInstitutionEndYear;

// Bloes up here tryign to do an insert on the studentCourseJoin.Institution. But if I removed this one, then it will blow up on another one.
context.SaveChanges(true);

}

If anyone has ANY ideas please, they would help alot.

A: 

Try adding those lines before calling SaveChanges:

ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(studentCourseJoin);
entry.ChangeState(EntityState.Modified);

Update:

Try this for Institution instead:

studentCourseJoin.Institution = context.Institutions.FirstOrDefault(i => i.Code == viewModel.StudentDetails.InstitutionCode);
Yakimych
Hi, thanks for the reply, there doesnt seem to be a ChangeState method??
Nicholas Mayne
Yes, sorry, that's EFv1 you're working with. Try entry.SetModified() instead.
Yakimych
No still not working. The studentCourseJoin object is set as modified, but the relationships i have defined above say added, which i thought is correct.I have checked the database and the values exist in the reference tables. ideas?
Nicholas Mayne
So what exactly is the error message you are getting now?
Yakimych
The error message is that its asking for insert permissions. When It shouldnt be doing an insert at all. I have try the above to see if that helps. But the same error persists.Its really weird.
Nicholas Mayne
If I understand correctly, an Institution object with the given Code already exists in the DB. If that is the case, see the updated answer and give it a try.
Yakimych
Awesome dude. It appears to be working :) Marked as answer... But jsut out of curriosity. Any reason why my example wasent working when it works perfectly fine with other objects?
Nicholas Mayne
When you create a new Institution object in your code, EF doen't have a way of knowing whether it's a new one or an existing one, so it tries to add by default. By retrieving it first, you tell EF exactly which entity you are connecting to your object. Regarding the other objects - it's hard to tell why it works without additional information regarding your DB structure and mappings (as well as the contents of the DB when running this code).
Yakimych
Awesome. Thanks for the help dude. I really appreciate it.
Nicholas Mayne
Sure. Good luck!
Yakimych