I am using EF 6 database modal in my application. I have a table TBL_USER
which has 1:N
relationship in some other table. One of them is TBL_USER_CASE
where primary key of TBL_USER
act as foreign key in TBL_USER_CASE
.
Now i am deleting some user from TBL_USER
. Before that i need to delete corresponding entries in TBL_USER_CASE
. I am using following code for that
private long DeleteUser(long UserID)
{
using(VerbaTrackEntities dataContext = new VerbaTrackEntities())
{
TBL_USER user = dataContext.TBL_USER.Where(x => x.LNG_USER_ID == UserID).SingleOrDefault();
if(user != null)
{
foreach (var cases in user.TBL_USER_CASE.ToList())
{
user.TBL_USER_CASE.Remove(cases);
}
}
dataContext.SaveChanges();
}
return 0;
}
Here i m getting exception
Additional information: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted
How can i do this operation correctly ??