L2E delete exception

Posted by 5YrsLaterDBA on Stack Overflow See other posts from Stack Overflow or by 5YrsLaterDBA
Published on 2010-04-15T19:21:06Z Indexed on 2010/04/15 19:23 UTC
Read the original article Hit count: 516

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ