none
How do i change the connection instance from ObjectContext keeping all entities attached ? RRS feed

  • Question

  • Hello,

    my Team is developing a Persistence Framework following same patterns like Unit Of Work
    and Repository.

    This Framework uses the ORMs NHibernate and Entity and one of its goals is become abstract
    the ORM use to upper layers.

    There is one implementation in this Framework where i need to change the connection (IDbconnection)
    from Context (ObjectContext) and i can't lose my context values. I want to keep all entities with
    its originals states after close the connection attached in Context and i want to associtate one new
    connection to this context.

    In my implementation to NHibernate i've done this way:

    NHibernate Implementation:

    session.Disconnect();          

    session.Reconnect(NewConnection);

    I would like to know if there is one way to realize one similar solution in the Entity Framework.

    Temporarily, i'm taking the following solution in the Entity Framework.

    I create one new ObjectContext with one new Connection and i am trying copy all values from older context
    to new context. I did it this way:

    var entries = contextFrom.Context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added | System.Data.EntityState.Modified | System.Data.EntityState.Deleted);

    This solution is more expensive and would be better only change the connection the context.

    Thank you for help.
    Tuesday, October 25, 2011 5:24 PM

All replies

  • Hi Cristiano,

    Welcome!

    According to your description, there is not a good way in Entity Framework now. I think your workaround is a way to handle your scenario---- Detech all the records from one objececontext and attach to another one. 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, October 26, 2011 9:01 AM
    Moderator
  • Hello,

    As I know you cannot change connection for existing context. Context itself doesn't allow it, Entity connection doesn't allow it as well and store connection most probably doesn't allow changing connection string.

    Also I have very strong doubts that your temporary solution works in all cases. You cannot have entities attached to two context and once you detach entity from the first one you will lose all information about its state and moreover all relations will be broken. The only options is to make deep copy of whole entity graphs attach them to new context and synchronize states of entities and relations in both contexts.

    This seems like requirement completely not suitable for EF and it leads to common summary: your abstract interface for data access can have only features shared by all wrapped technologies.

    Best regards,
    Ladislav

    Wednesday, October 26, 2011 9:08 AM
  • Hi,

    I am writing to check the status of the issue on your side.Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.I will be more than happy to be of assistance.

    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, October 31, 2011 2:14 AM
    Moderator
  • Hi Alan,

    we are using  this way suggested in our solution however we are concerned about the effects this solution. We 've done several tests and we doesn't found problem yet.

     

    I would like to know if in the future the Entity Framework Team will implement something to change the conection used by context.

     

    Thank you fo help.

    Have a nice day.

    Tuesday, November 1, 2011 1:38 PM
  • Hi Cristiano,

    Thanks for your feedback.

    I didn't see any plan for this in EF team blog: http://blogs.msdn.com/b/adonet/ 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, November 2, 2011 8:05 AM
    Moderator