TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kacey Ezerioha
NA
37
0
Exception On Foreign Constraint
Oct 15 2012 8:54 AM
Hi,
I have a table
StaffLevelMapping
that has a foreign key column, which is a primary key another table
AcademicLevel
. It raises an exception that the Insert statement conflicted with a foreign key and it has its reference on the foreign key and the table in which it's a primary key. I know this usually happens if the value you are inserting does not exist in it's original table. But this does exist actually. I tried inserting it directly in sql server and it worked perfectly. But through my code it doesn't work. What surprises me the most is I used the same insert logic for the rest of my tables and I don't have problems save this one with this very table. Please I need a very quick help on this I have not slept in 2 days because of this error.
Here's my insert code below:
public static StaffLevelMapping AddStafflevel(int staffId, int levelId, bool isEnabled)
{
var context = ObjectContextHelper.CurrentObjectContext;
var staffLevel = context.StaffLevelMappings.CreateObject();
staffLevel.StaffID = staffId;
staffLevel.ID = levelId;
staffLevel.IsEnabled = isEnabled;
context.StaffLevelMappings.AddObject(staffLevel);
context.SaveChanges();
return staffLevel;
}
Many thanks in advance to that special person that can put me out of this misery.
Reply
Answers (
1
)
How to show distinct values in to Combo Box from data base
Unable to open Excel sheet – Asp.net C#