0

How can I use a mdf file that should be located in the solution directory (or elsewhere) in combination with Entity Framework? By default EF creates the file in "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA". Additionally it would be nice to be able to access the database over a windows peer-to-peer network after staring the .net application which initializes the server instance.

I guess i have to use a connection string something similar to this: http://msdn.microsoft.com/en-us/library/bb264564%28v=sql.90%29.aspx, which uses User Instance and AttachDbFilename

This post states that i can use connection strings as usual with EF: http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx

What i get when using my own (not the default EF) connection string is: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. (Configuration system failed to initialize)

<configuration>
  <connectionStrings>
    <add name="SomeContext"
      providerName="System.Data.SqlClient"
      connectionString="Server=.\SQLEXPRESS;
      AttachDbFilename=C:\temp\SomeNamespace.SomeContext.mdf; 
      Integrated Security=True;
      User Instance=True" />

  </connectionStrings>
...

Versions used:

SQL Server Express Edition R2 SP1

EF 4.3.1 (Code First)

user764754
  • 3,865
  • 2
  • 39
  • 55

1 Answers1

0

Thanks to these answers I was able to use a custom location:

Problem connecting to database - user instance and Entity Framework issue

WPF and ADO.NET EF - error part II

My steps were:

  • Copying the .mdf and .LDF files that were created by EF to somewhere i wanted them to be
  • Starting Microsoft SQL Server Management Studio (Express) as admin and deleting the old database with SSMS (which has been copied)
  • Attaching the copied .mdf file to the local server with SSMS
  • Restructuring App.config so the configSections stands before the connectionStrings section
  • Changing the connection string (it doesn't use user instances anymore that are deprecated anyway)

My App.config now looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=u57a3c561234h089" />
  </configSections>

  <connectionStrings>
  <add name="SomeContext"
      providerName="System.Data.SqlClient"
      connectionString="Server=.\SQLEXPRESS; Integrated Security=True; Database=SomeNamespace.SomeContext" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory 
    ...
Community
  • 1
  • 1
user764754
  • 3,865
  • 2
  • 39
  • 55