Open database by code

4 posts, 0 answers
  1. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 15 Mar 2011 Link to this post

    Hi!

    I'm developing in XAML (Window), and I'm noting first time application window runs and is binding elements against ORM assembly (where Entity classes generated from database) fields, window takes more or less 4 or 5 seconds to show elements with data, next time (without exit) same window or another is loaded, time is faster to show window. I'm using a class derived from OpenAccessContext to make any operations (created automatically by OpenAccess). how can I do to open database (I believe caused by this) and close it by code?


    thanks
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 15 Mar 2011 Link to this post

    Hello Ramiro,
    The database is opened if the first context is accessing the database the first time. We are calculating the metadata in that call so that it is usually longer. What do you want to achieve?

    Kind regards,
    Jan Blessenohl
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft banner
  4. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 12 Apr 2011 Link to this post

    HI!

    I would like to avoid user receive the impact of have to wait first time, so when main app is loading I would like to open database manually at that time. If it is possible I would like to. thanks

  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 15 Apr 2011 Link to this post

    Hello Ramiro,

    Yes, this is possible. In the class deriving from OpenAccessContext you should have code similar to this:
    private static string connectionStringName = @"AdventureWorksConnection";
    private static BackendConfiguration backend = GetBackendConfiguration();
    private static MetadataSource metadataSource = XmlMetadataSource.FromAssemblyResource("EntityDiagrams1.rlinq");
     
    ...
     
    public static BackendConfiguration GetBackendConfiguration()
    {
         BackendConfiguration backend = new BackendConfiguration();
         backend.Backend = "mssql";
         return backend;
    }

    Those objects are needed to open the database in the context's constructor but you can instantiate them before that and open the database manually. The code you would need is this:
    BackendConfiguration backend = new BackendConfiguration();
    backend.Backend = "mssql";
     
    Assembly domainModelAssembly = typeof(DatabaseLog).Assembly; //Needed if the persistent classes are placed in another assembly; DatabaseLog is one of the persistent classes.
     
    MetadataSource metadataSource = XmlMetadataSource.FromAssemblyResource(domainModelAssembly, "EntityDiagrams1.rlinq");
     
    Database d = Database.Get("AdventureWorksConnection", backend, metadataSource.GetModel());

    This would initialize the database instance and associate it with the metadata of the EntityDiagrams1.rlinq model and the creation of the first OpenAccessContext should be much faster.

    If you would also like to dispose the database at the end, you can use the following code:
    Database database = Database.Get("AdventureWorksConnection");
    database.Dispose();
    I hope that helps.

    All the best,
    Alexander
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top