none
Entities in 'Context.Files' participate in the 'File_Pack' relationship. 0 related 'File_Pack_Target' were found. 1 'File_Pack_Target' is expected.

    Question

  • Hello,

    I am trying to delete a few entities in EF 5 by their IDs as follows:

    Context context = new Context();
    Pack pack = context.Packs.First(x => x.Id == 12);
    
    context.Entry<Pack>(pack).Collection(x => x.Files).Load();
    
    foreach (Int32 id in ids) {
      File file = new File { Id = id };
      context.Files.Attach(file);
      context.Entry(file).State = EntityState.Deleted;
      context.SaveChanges();
    }
    

    But I get the following error:

      Entities in 'Context.Files' participate in the 'File_Pack' relationship. 0 related 'File_Pack_Target' were found. 1 'File_Pack_Target' is expected.

    Does anyone knows why I am getting this error?

    Thank You,

    Miguel

    Thursday, December 13, 2012 9:17 PM

Answers

  • Hi Miguel,

    Welcome to the MSDN forum.

    Do you want to the Files with the Id in ids?

    Is the table Pack and File the one to many relationship?

    Please check whether the following works:

                    foreach (Int32 i in ids)
                    {
                        var f = context.Files.Where(u => u.Id == i).First();
                        context.Files.Remove(f);
                    }
                    context.SaveChanges();
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, December 26, 2012 2:35 AM
    Monday, December 17, 2012 4:59 AM