Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Application ignoring AppDomain.CurrentDomain.SetData value
    primarykey
    data
    text
    <p>I have a solution (All .NET 4.5 projects) which has a windows service, a WPF UI application and a data layer in which I have my EDMX model and a generic repository.</p> <p>When I debug the service, data ends up in the database and I want to UI to use the same database file when I launch it.</p> <p>So I created a directory like such:</p> <pre><code>[solution root directory]\Data </code></pre> <p>In which I put the MDF and LDF in. I made sure my EDMX model uses this MDF file when I do updates and such.</p> <p>For the service and the UI to use the same MDF file, I made sure their connection string use the <code>|DataDirectory|</code> symbol and do this when they both start:</p> <pre><code>public MainWindow() { string dataDir = Utilities.Utilities.ReturnDataDirectory(System.Reflection.Assembly.GetExecutingAssembly()); AppDomain.CurrentDomain.SetData("DataDirectory", dataDir); InitializeComponent(); Loaded += MainWindow_Loaded; } </code></pre> <p>Here's the code for <code>ReturnDataDirectory</code>:</p> <pre><code>public static string ReturnDataDirectory(Assembly referingAssembly) { string curDir = referingAssembly.Location; string dataDir = string.Empty; int x; var curDirArray = curDir.Split('\\'); //Return a directory without the 'Solution.Name\bin\Debug' part... for (x = 0; x &lt; (curDirArray.Count() - 4);x++ ) { dataDir += string.Format("{0}\\", curDirArray[x]); } return string.Format("{0}Data", dataDir); } </code></pre> <p>This generates the exact directory where the MDF file indeed resides so the problem is not there, AFAIK.</p> <p>Yet, when I start the WPF UI, I get this error:</p> <pre><code>An attempt to attach an auto-named database for file C:\\Dropbox\\My Docs\\MySolution\\MySolution.UI\\bin\\Debug\\MySolution.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. </code></pre> <p>Why on earth is it still trying to look there after setting <code>|DataDirectory|</code> correctly ? There is no MDF file there anyways, and that's how I intend it for the moment being.</p> <p>Here is the connection string used in the app.config of the WPF UI ap:</p> <pre><code>&lt;add name="MySolutionEntities" connectionString="metadata=res://*/MySolution.csdl|res://*/MySolution.ssdl|res://*/MySolution.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MySolution.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt; </code></pre> <p>What am I missing ?</p> <p>Thank you!</p> <p><strong>EDIT</strong>: This approach works for the Windows Service project, but not for the WPF UI application. Is it because I'm setting the <code>|DataDirectory|</code> too late ? I also tried sniffing something use Process Monitor, and I can't pickup the UI application trying to access an MDF whatsoever :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload