locked
POCO: delete object from "zero or one" relation RRS feed

  • Question

  • Hi

    How can a client delete an object from a zero or one relation?

    I have A (1) <-> (0..1) B

    A.B = null; doesnt work

    Using EF4

    Best regards

    Michael 



    • Edited by winmike Wednesday, September 14, 2011 8:03 PM
    Wednesday, September 14, 2011 7:57 PM

All replies

  • Hello,

    it should work. Are you sure that B is loaded prior to setting it to null? Try this:

    using (var context = new Context())
    {
        var a = context.As.Include(a => a.B).Single(...);
        var b = a.B;
        a.B = null;
        context.Bs.Remove(b); // This should not be needed
        context.SaveChanges();
    }
    
    Best regards,
    Ladislav

    • Proposed as answer by Jackie-Sun Wednesday, September 21, 2011 8:45 AM
    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:22 AM
    • Unmarked as answer by winmike Thursday, September 29, 2011 6:09 AM
    • Unproposed as answer by winmike Thursday, September 29, 2011 6:09 AM
    Thursday, September 15, 2011 8:41 AM
  • Hi

    Thanks for reply. I dont have a context availible on client - i just have the POCO ...

    Regards

    Michael 

    Thursday, September 15, 2011 6:51 PM
  • Hi Michael,

    You should have an objectcontext or Dbcontext if you use EF4 code first. Please check your code and take Ladislav's advice.

    Here is a blog article written by ScottGu, which is about how to develop using EF code first. http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Monday, September 26, 2011 6:22 AM
    • Unmarked as answer by winmike Thursday, September 29, 2011 6:08 AM
    Wednesday, September 21, 2011 8:44 AM
  • Hi

    No - i use POCOs (STE) on a client (WCF) which doesnt know EF

    Regrads
    Michael

    Tuesday, September 27, 2011 5:32 AM