Forum


HomeHomePremiumPremiumDevelopmentDevelopmentRepository Reuse In External ApplicationRepository Reuse In External Application
Previous
 
Next
New Post
3/20/2015 12:08 PM
 

I have a project based on the Chris Hammond, Christoc module template.  I have a ton of code that I use to access data an external database.  In my repositories I change the database from the default to whichever I need for that particular object.  I do so with code that looks like this:

using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY))
{
    var rep = ctx.GetRepository<Product>();
    products = rep.Get().ToList();
}

The default database is switched in the call to .Instance().  The repositories are used by my custom DNN modules.  The repository is part of the solution that contains multiple custom modules.  When I compile and install using the Extensions part of DNN, everything works well.  In the code above, MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is found in a file MyModuleSettingsBase.cs file of my module solution.  It is set to a simple string like "ProductDatabase".  In the solution for the base DNN install (not the module solution), within the web.config file, there is a value in <connectionStrings> with name="ProductDatabase" which contains the actual connection string.  This all links up fine on the DNN website.

 Now I am writing a console application that does some monitoring of the site.  I want to access the database to check values in the product table.  I would like to reuse all of the repository code I have written.  In an attempt to do so, I added a reference to the MyModules.dll file so I would only have one copy of the base code.  This works to give me access to all the objects and the associated repositories  but when I attempt to query data it fails.  When debugging I can see that it fails on the line:

using (IDataContext ctx = DataContext.Instance(MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY)) 

When viewed in a debugger, the string value MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY is correctly set to "ProductDatabase" but the code is unable to link this with the actual connection string.  I don't know where it would be checking for the connections string when running from my console application.  I attempted to put a <connectionStrings> section into my App.config file but this didn't do the trick.

Where can I set the value of my connection string so it matches up to the key value stored in MyModuleSettingsBase.DATABASE_CONNECTION_STRING_KEY?


 
New Post
3/21/2015 6:22 AM
 

Andy,

I think you are on the right track.  DotNetNuke.Data.DatContext is taking the name of the connection string and passing it to PetaPoco.  It should be the name of the connection string key related to the current application.  So in the case of the DNN website, it would be the web.config.  But for your console application, it should find it in the app.config.   I would think it would find it if you have an app.config in the same folder as your console exe and it has a section that looks like this:

<connectionstrings>
    <add name="ProductDatabase" connectionstring="Data Source=.;Initial Catalog=ProductDatabase;Integrated Security=SSPI;" providername="System.Data.SqlClient" />
</connectionstrings>

I would think you also need a reference in your console application to DotNetNuke.dll too.

 
Previous
 
Next
HomeHomePremiumPremiumDevelopmentDevelopmentRepository Reuse In External ApplicationRepository Reuse In External Application



Try FREE
30 days money back guaranteed