Context creation time is too long

2 posts, 0 answers
  1. Kemal
    Kemal avatar
    1 posts
    Member since:
    Feb 2013

    Posted 03 Dec 2015 Link to this post

    Hi,

    My context have more than 600 entity (table & view).

    Context creation time is approximately 8sn.

    It's too long for request based context creation model.

    How can i reduce creation time?

    Thanks.

  2. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 08 Dec 2015 Link to this post

    Hi Kemal,

    long time for creating a context instance is an indication that you do something extra. Typical reason for such timing could be code like this:

    public partial class TestPolimorphicRefsContext : OpenAccessContext
        {
            static MetadataSource metadataSource = new TestPolimorphicRefsMetadataSource();
            static BackendConfiguration backendConfiguration = new BackendConfiguration()
            {
                Backend = "mssql"
            };
     
            public TestPolimorphicRefsContext()
                : base(DbConnection, backendConfiguration, metadataSource)
            {
     
            }
    }

    The above code creates once a MetadataSource instance and passes it each time to the constructor when it gets invoked. In this case the constructor calls a method which prepares the metadata each time, if you have lots of entities in the metadata this could be costly operation. If you have this patter in your code I would suggest you to change to the following:
    public partial class TestPolimorphicRefsContext : OpenAccessContext
        {
            static MetadataContainer metadataContainer = new TestPolimorphicRefsMetadataSource().GetModel();
            static BackendConfiguration backendConfiguration = new BackendConfiguration()
            {
                Backend = "mssql"
            };
     
            public TestPolimorphicRefsContext()
                : base(DbConnection, backendConfiguration, metadataContainer)
            {
     
            }
    }

    Here as you can see the complete metadataContainer is prepared upfront once and then it is re-used in each and every context created after that. The first creation would still be slightly longer, might be comparable with the timing you mention but all next creations should be almost immediate.

    PS: have in mind that if you apply some logic and modify the metadata inside the FluentMetadataSource the approach above will not work as the resulted metadataContainer will be cached the first time. 

    Hope this helps. 

    Regards,
    Kaloyan Nikolov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
Back to Top