locked
Error 'temporary value while attempting to change the entity's state to 'Deleted'. RRS feed

  • Question

  • User-1355965324 posted

    When I try to delete the record using repository , the following error is being showed . Please can you the reason for this error and how can I fix this problem . The record is not deleted. The table has no primary key with Id. But  It has multiple column primary key 

    context.UserDepartment.Remove(model);  
    goContext.SaveChanges(); // throws exception

    The property 'UserDepartmentLinkID' on entity type 'UserDepartLink' has a temporary value while attempting to change the entity's state to 'Deleted'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.

    Pol

    Wednesday, August 5, 2020 1:28 PM

All replies

  • User1120430333 posted
    To me, it is saying that UserDepartLink was never persisted to the database where an ID could be assigned to a persisted record in the database table. If the record is not there in the database, then how can it be deleted by its ID?

    Under what circumstances are you trying to delete the record from the database table?
    Wednesday, August 5, 2020 1:57 PM
  • User-1355965324 posted

    I am trying to remove  the record by calling the linq. But When execute this line the error is being cateched in Exception 'Data is Null. This method or property cannot be called on Null values. There is  record already in table and there is no any null in UserID, DepotNo, DepartementID

     public void Delete(GoUserDepartLink link)
            {
    
                try
                {
    
                    var delobj = goContext.goUserDepartment.Where(p => p.UserID == link.UserID
                                                                  && p.DepotNo == link.DepotNo
                                                                  && p.DepartmentID == link.DepartmentID).SingleOrDefault();
                    goContext.goUserDepartment.Remove(delobj);
                }
                catch (Exception ex)
                {
    
                }

    Wednesday, August 5, 2020 3:26 PM
  • User475983607 posted

    polachan, why do you have an empty catch block?  That's a terrible design.  Use the the standard debuggers and provide the following information.

    What are the values of the "link" model properties?

    What are the values of the "delobj" model properties?

    Use the network tools in dev tools to copy the post collection and share that information in the forum.  Also share the HTML form and models so the community can verify property and input names.  

    I'm pretty sure once you perform the basic troubleshooting steps above you'll be able to figure out the bug on your own.  If you want community debugging support then make an effort to provide code the community can run which reproduces your results.

    Wednesday, August 5, 2020 3:54 PM
  • User1120430333 posted
    public void Delete(GoUserDepartLink link)
            {
    
                try
                {
    
                    var delobj = goContext.goUserDepartment.Where(p => p.UserID == link.UserID
                                                                  && p.DepotNo == link.DepotNo
                                                                  && p.DepartmentID == link.DepartmentID).SingleOrDefault();

    if (delobj != null)
    goContext
    .goUserDepartment.Remove(delobj
    );
    } catch (Exception ex) { }

    The null value check should be done. If the object is not retrieved based on the query criteria on a SingleorDefault(), the object is going to be null (the default value),  and the object was not found based on the criteria to retrieve the object. 

    Maybe, you need to find out why a null valued object is being returned. 

    Wednesday, August 5, 2020 7:09 PM
  • User-17257777 posted

    Hi polachan,

    polachan

    'Data is Null. This method or property cannot be called on Null values.

    Looks like the link object is null, debug to if the parameter(GoUserDepartLink link) is successfully bound.

    Best Regards,

    Jiadong Meng

    Thursday, August 6, 2020 8:15 AM