locked
At least one related end in the relationship could not be found. RRS feed

  • Question

  • Hi,

    when i am trying to attach entity to an object context it returns InvalidOperationException error : "At least one related end in the relationship could not be found."

    I not able to figure it out why. I am trying to do :

    Employee emp = new Employee();

    emp.Name="Bob Warmer";

    emp.Salary=$6500;

    emp.Addresses.Add(new Address(){AddressLine1="askdjfas",AddressLine2="jkaldkjlkdjf"});

    context.Employees.AddObject(employee);

    context.SaveChanges();

    //After that i get the emp.EmployeeID field with value / primary key

    //After once data pushed into the database, I binary Serialized the employee object and put into in xml envelop

    //for further edit / update operation i use data from xml envelop, and update to database only when data is approved

    //During approval process, i simply pick the object from xml envelop and attached to objectcontext like

    using(DataModel context = new DataModel()){

    Employee emp = GetDataFromXml(Int32 empId);

    context.Employees.Attach(emp);//this line throws InvalidOperationException

    context.SaveChanges();

    }

    If i detach the Object before serialzed then all works well but child objects are clear after detach operation which is another problem.

    Please help, or let me know if i missed out something.

    Wednesday, June 20, 2012 3:45 PM

Answers

  • Hi Karan Sharma1,

    Welcome to MSDN Forum.

    Please check whether the related entities have already exist in the database. I think it caused by the child objects are not exist in database, so when you do attach operation, the exception is thrown. If detach the entity before serializing, the child objects are cleared, so the exception will not thrown.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Allen_MSDN Sunday, July 1, 2012 9:31 AM
    Thursday, June 21, 2012 8:51 AM

All replies

  • Hi Karan Sharma1,

    Welcome to MSDN Forum.

    Please check whether the related entities have already exist in the database. I think it caused by the child objects are not exist in database, so when you do attach operation, the exception is thrown. If detach the entity before serializing, the child objects are cleared, so the exception will not thrown.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Allen_MSDN Sunday, July 1, 2012 9:31 AM
    Thursday, June 21, 2012 8:51 AM
  • Hi Allen Li,

    First of all i would like to thanks for your nice response.

    I have checked the child objects they are many like around 40 but i was able to verifies around 20-25. It's very hard to debug which one causes error.

    If i detach the entity object before serializing then i have to make seperate call to fetch all referenced table / child object to shown on UI.

    If there is any way by through i quickly find out which child object causes error then it will be great helpfull. I go through the exception which but fails to find the dirty child object.

    Thanks again for your nice support!

    Karan Sharma


    Karan Sharma System Analyst

    Thursday, June 21, 2012 8:11 PM
  • Hi Karan Sharma1,

    I think you can check the total count first. Try to query out the total count of the child entities in the 'emp' entity, and then query out the total number in the database, check if the total number is same.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 25, 2012 7:52 AM