close connection on databse before restore

2 posts, 0 answers
  1. Richard
    Richard avatar
    9 posts
    Member since:
    Sep 2011

    Posted 21 Sep 2011 Link to this post


    I want to made a database restauration with the smo microsoft component, but the database is not in exclusive mode ?
    I made (I work with c# - SQL 2008 R2):
    1 - I made a dispose on my dbcontext. Like this : 


    instance.m_Diagram = null;
    2-I call the Garbage collector
    3-I made my database restauration
    ==> but the database restauration doesn't works, I thing that my context still exist !
    (==> the restauration doesn't work because I can't access to the database in exclusive mode with the smo component, I think that there is again a connection with my diagram or context !)
    Please, do you have a solution to be sure that my "diagram" or "context" is close ?
    In advance,



  2. Zoran
    Zoran avatar
    534 posts

    Posted 23 Sep 2011 Link to this post

    Hi Richard,

     The context is managed by an underlying Database object. If you want to be sure all of your connections  are closed I suggest you to dispose of the Database object  after you dispose your context. You can get that database object via your context, however the access to it is protected so you should expose it in a partial class.

    public partial class MyContextClass
        public Database GetDatabase()
                return  this.GetScope().Database;       

    Then you can dispose it after you dispose your context:
    public void DisposeContextAndDatabase()
        Database db = myContextInstance.GetDatabase();

    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
Back to Top