This is a migrated thread and some comments may be shown as answers.

close connection on databse before restore

1 Answer 58 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard
Top achievements
Rank 1
Richard asked on 21 Sep 2011, 12:38 PM
Hi,

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.Dispose();

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,
Thanks,
Richard.

 

 

1 Answer, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 23 Sep 2011, 04:04 PM
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();
    myContextInstance.Dispose();
    db.Dispose();
}

Greetings,
Zoran
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 >>

Tags
Development (API, general questions)
Asked by
Richard
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Share this question
or