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

Config file interaction not working with two intertwined models

2 Answers 21 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.
Cory
Top achievements
Rank 1
Cory asked on 09 Nov 2015, 07:48 PM

Hello,

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.

Thanks,

-Cory

2 Answers, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 12 Nov 2015, 10:44 AM
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. 

Regards,
Kaloyan Nikolov
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Cory
Top achievements
Rank 1
answered on 12 Nov 2015, 07:48 PM

Awesome, I will give both a try.

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

-Cory

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