Hi,
I m' lately trying to set up a SQL Server Compact Edition connection to my AutoCAD plugin. My goal is to have one db object that i will deploy with my plugin, so that i do not have to install MS SQL Server on every machine i want to deploy to.
My current setup is a Code first ADO.net with Entity Framework. Initially I was trying to do the following:
public MyDbContext() : base("name=MyContextString")
{
}
This code above gave me an error that the connection string does not exist in the App.config file.
I found a post that says that since I m developing a plugin. Acad.exe reads from Acad.exe.config and not my App.Config.
A solution would be to alter the acad.exe.config, but i was hoping for a solution where i could set my connection string with provider name from code.
Does anyone have a working example of a code based database configuration for a SQL Compact Edition database?
I am with MikeD on using SQLite instead of SQL Compact, but you would still have the same issue (of loading connection string from app.config).
While in .NET add-in DLL project you can add an app.config file, the code using System.Configurations.ConfigurationManager would not read the configuration data in your DLL project's app.config (its build output should be [Add-in Name].dll.config) without special code. In earlier .NET (1.x), all the configuration content in DLL's app.config should be merged into app.config of the EXE app that loads the DLL. IN AutoCAD, that is Acad.exe.config.
So, yes, you can merge the DLL's app.config into acad.exe.config.
However, with later .NET (I do not remember since when, 2.x or 3.x?), you can directly load configuration in dll's *.dll.config in some way, the only as System.Configuration.Configuration.AppSettings.
For example, in your DLL's app.config, you can add appSettings like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="MyDBName" value="Database Name" />
<add key="MyDBFileName" value="C:\MyData\MyDBFile.mdb" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
When you build the project, you get [Add-in name].dll.config, you place it in the same folder as the DLL. You can have code like this to load the settings:
private void LoadSettings(out string dbName, out string fileName)
{
dbName="";
fileName="";
Configuration config = null;
string exeConfigPath = this.GetType().Assembly.Location;
try
{
config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
}
catch (Exception ex)
{
throw new System.IO.FileFormatException( $"Loading configuration error: {ex.Message}");
}
if (config != null)
{
dbName = config.AppSettings.Settings[MyDBName].Value;
fileName = config.AppSettings.Settings[MyDBFileName].Value;
}
else
{
throw new InvalidProgramException
( "Invalid *.dll.confg file.");
}
}
If the settings is per user based, you can also simply add settings in project->Resource->Settings and add settings in User scope. Then you can simply load the settings with
Properties.Settings.Default.xxxxx (make sure you give it default value when the code runs the first time, and save the settings at some point).
HTH