Clarification Questions on Multiple Model Handling

Thread is closed for posting
1 posts, 0 answers
  1. Jeff
    Jeff avatar
    11 posts
    Member since:
    Oct 2010

    Posted 06 Apr 2017 Link to this post


    I am currently working to resolve an issue that our clients occasionally experience where the model being loaded in the background on IIS throws a "Failed to obtain a Database object.  There is already an opened context with same ConnectionName/ConnectionString..."

    From my reading of the documentation, it appears that there are basically two options to resolve this. What is not clear is which one is appropriate to use at which time.

    Option 1: Override the CacheKey

    This options uses a partial class of the model to override the CacheKey property as follows:

    01.public partial class MyModel
    03.    protected override string CacheKey
    04.    {
    05.        get
    06.        {
    07.           // This uses the namespace as the "uniqueness" and works
    08.           // if all the models have the same connection string.
    09.           return typeof(MyModel).Namespace;
    10.        }
    11.    }

    So the code snippet above works for Models that are in the same Namespace, and are using all the same connection string.  BUT, that doesn't always hold true, particularly if you are created web front end for multiple databases...  which leads to the next Option

    Option 2: Override the model's Init() method

    This option also uses a partial class, however, you can set a unique CacheKey for each connection string, allowing you to have multiple models with corresponding metadata in the backend that connect to the same database.

    01.public partial class MyModel
    03.    protected override void Init(string connectionString, string cacheKey, Telerik.OpenAccess.BackendConfiguration backendConfiguration, Telerik.OpenAccess.Metadata.MetadataContainer metadataContainer, System.Reflection.Assembly callingAssembly)
    04.    {
    05.        // Each connectionstring generates its own cachekey, so models connecting on the same connectionstring can share metadata
    06.        // while those connecting on different connections get their own.
    07.        cacheKey = "MM" + connectionString;
    08.        base.Init(connectionString, cacheKey, backendConfiguration, metadataContainer, callingAssembly);
    09.    }

    So the code above works where I want to have multiple models connecting to the same database, but maintain separate metadata when connecting to a different databases.

    What I want to know / discuss, is the following:

    1.  Is my understanding of how the CacheKey affects the DataAccess Metadata aggregation correct?

    2.  If not, what is the better way of handling the scenario of multiple models in different namespaces connecting to the same database (preferably without needing to write code to manage the MetadataContainers manually)?

    3.  If my understanding is correct, are there details that I have glossed over or missed that I should consider, and if so, what are they?


    Anyway, thank you in advance for the discussion.




Back to Top