none
Where can I find state of independent associations in ChangeTracker (EF 4.1)?

    Question

  • Hello,

    when using EDMX + ObjectContext I can use ObjectStateManager to get insatances of ObjectStateEntry representing independent associations. How can I do the same in DbContext API without converting to ObjectContext? How can I change state of independent association in DbContext API?

    Best regards,
    Ladislav

    Thursday, March 17, 2011 8:59 AM

Answers

  • Hi,

    I'm back!

    You will need to drop down to ObjectContext (using ((IObjectContextAdapter)myDbContext).ObjectContext) to get the relationship entries for IAs. Dropping down to ObjectContext also gets you access to ChangeRelationshipState which works for IAs. If you do this, then you will need to deal with all the complexities of working with IAs. If you find yourself needing to manipulate IAs then there is a very good chance that things will get much easier if you can expose FKs in your models instead.

    DbContext API doesn't cover this.

    Have a nice day.


    Alan Chen[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.

    Monday, March 28, 2011 1:38 AM

All replies

  • Hi Ladislav,

    Welcome!

    I think you may to want this:

    var state= context.Entry(object);
    
    Have a nice day.
    Alan Chen[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.

    Friday, March 18, 2011 8:00 AM
  • Hello Alan,

    I'm not sure I understand your solution. I know that I can use Entry to get a state of entity but how can I use it to get a state of independe association? Perhaps the question is how can I get an object representing independent association in DbContext API without converting back to ObjectContext?

    Best regards,
    Ladislav

    Friday, March 18, 2011 9:23 AM
  • Hi Ladislav,

    I willl consult the product team about your question, I will come back if there is any information. Thanks for understanding.

    Have a nice day.


    Alan Chen[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.

    Wednesday, March 23, 2011 7:01 AM
  • Hi,

    I'm back!

    You will need to drop down to ObjectContext (using ((IObjectContextAdapter)myDbContext).ObjectContext) to get the relationship entries for IAs. Dropping down to ObjectContext also gets you access to ChangeRelationshipState which works for IAs. If you do this, then you will need to deal with all the complexities of working with IAs. If you find yourself needing to manipulate IAs then there is a very good chance that things will get much easier if you can expose FKs in your models instead.

    DbContext API doesn't cover this.

    Have a nice day.


    Alan Chen[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.

    Monday, March 28, 2011 1:38 AM
  • Thanks Alan,

    unfortunately FKs are not possible in many-to-many relations.

    Best regards,
    Ladislav

    Monday, March 28, 2011 3:13 PM
  • Some Answers from MS-Moderators are simply useless... head->desk
    Saturday, April 02, 2011 11:49 AM