views:

41

answers:

0

I have following code to delete an user from database:

         try
            {
                var user = from u in db.Users
                           where u.Username == username


               select u;

            if (user.Count() > 0)
            {
                db.DeleteObject(user.First());
                db.SaveChanges();
            }
        }

but I got exception like this:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
   at System.Data.Objects.ObjectContext.SaveChanges()
   at MyCompany.SystemSoftware.DQMgr.User.DeleteUser(String username) in C:\workspace\SystemSoftware\SystemSoftware\src\dqm\User.cs:line 479

The Users table is referenced by few other tables. It is probably caused by the foreign key constraint?