Config file interaction not working with two intertwined models

Thread is closed for posting
3 posts, 0 answers
  1. Cory
    Cory avatar
    13 posts
    Member since:
    Nov 2014

    Posted 09 Nov 2015 Link to this post


    I realize that the title probably doesn't make any sense, so here's my situation.

    I have two OpenAccess fluent models in my solution, each in its own project. The reason for this is that our database consists of many static, internally designed tables, and many dynamic, user-defined tables. The first model uses persistent classes for the known tables, and the second model uses artificial mapping for the unknown, user-defined tables.

    The important thing to note is that the information required for the dynamic model exists in ​two of the tables from the static model. There's one table that describes the user-defined tables, and one table that describes the columns for each of those tables. This means that we need to create an instance of the static model within the PrepareMapping() of the dynamic model, so that we know what to map. 

    I've achieved the above procedure with great success, but the problem is that I can't for the life of me figure out how to get the connection string/backend info from a configuration file. The solution refuses to build, and it seems to be entirely due to the fact that I'm creating an instance of the static model ​during the mapping of the dynamic model. 

    I'm guessing that this behaviour has something to do with the Enhancer, but I'm not sure how to get around it. If I hard-code the connection/backend info, then it builds and works no problem. It's only when I try to get that info from a config file that everything blows up. Also, I've tested the ConfigurationManager.ConnectionStrings["foo"].ToString() call to ensure that it's working and returning the intended value, so it's not that.

    Any ideas? I'm not sure how to pass a pre-loaded instance of the static model into the PrepareMapping() method or if that would even solve my problem.



  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 12 Nov 2015 Link to this post

    Hi Cory,

    Most probably the issue you experience is exactly because of the enhancement process. It creates an instance of all available fluent metadata sources and tries to create the metadata. In your case this leads to an attempt to communicate with the DB and as this call actually happens in the Ehnacer.exe process it looks up the connection string information in its config file where it is not present. 

    I would suggest you to decouple both contexts/fluent metadata sources as much as possible. What you can do is to add a property holding all needed information in the artificial fluent metadata source. It will be initialized to null or empty list and then at compile time you will not need to touch the DB. At run time the process would be: create an instance of the static context; load all needed mapping information and pass it to the artificial fluent metadata source. In this case both context will not know for each other. If you need to perform this workflow on multiple places you could wrap it in kind of service or a factory method so that it centralized and easy to consume. 

    PS: make sure you cache the resulting artificial MetadataContainer and pass the cached instance when you create a new context instance as the creation of artificial container by executing PrepareMapping each time is relatively expensive operation. 

    I hope this info helps. Should you have any further questions do not hesitate to get back to us. 

    Kaloyan Nikolov
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. Cory
    Cory avatar
    13 posts
    Member since:
    Nov 2014

    Posted 12 Nov 2015 in reply to Kaloyan Nikolov Link to this post

    Awesome, I will give both a try.

    The cached MetadataContainer tip is actually huge. Thanks for that!


Back to Top