Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-First with SQL Server Express ; Operating System Error 2

I'm trying to generate a SQL Server Express embedded database (.mdf) with code-first Entity Framework. It used to work fine but now, I just get over and over the same error, and I cannot identify the issue. The problem occurs on the initialization, it just hangs forever and if you look at intellitrace you can see some kind of loop of throw/catch statements all returning

"Unable to open the physical file "c:\gt\aspnetdb.mdf". Operating system error 2: "2 (failed to retrieve text for this error. Reason: 1815". Cannot attach the file "c:\gt\aspnetdb.mdf" as database 'WikDb'." (System.Data.SqlClient.SqlException)

UPDATE I also get the following error now just before the "unable to open physical file...."

Column 'InvariantName' is constrained to be unique. Value 'System.Data.SqlClient' is already present.

END UPDATE

Here is my app.config

<configuration>
   <system.data>
      <DbProviderFactories>
         <remove invariant="System.Data.SqlServerCe.4.0" />
         <add name="Microsoft SQL Server Compact Data Provider 4.0"
              invariant="System.Data.SqlServerCe.4.0"
              description=".NET Framework Data Provider for Microsoft SQL Server Compact"
              type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0"/>
      </DbProviderFactories>
   </system.data>
   <connectionStrings>
      <add name="WikDb"
           connectionString="data source=.\SQLEXPRESS;Integrated Security=True;
                       database=WikDb;AttachDBFilename=c:\gt\aspnetdb.mdf;
                       User Instance=True"
           providerName="System.Data.SqlClient" />
   </connectionStrings>
</configuration>

And here is my initializing code

Database.SetInitializer(new DbInitializer()); //DropCreateDatabaseAlways<WikDb> intializer
Database.DefaultConnectionFactory.CreateConnection( "WikDb" );
WikDb db = new WikDb();
db.Database.Initialize( true );

Any help appreciated

Thank you,

like image 672
Pierluc SS Avatar asked May 11 '26 00:05

Pierluc SS


1 Answers

It seems like your SQL Server Instance is broken. This tends to happen when installing additional instances pretty often - if using the Express Version that is, the full version is way easier to install and maintain (if you believe google - or my personal experience :P). Best way to see if thats the case would ofc to run your application on another machine with a working SQL Server instance. Can you connect to your database using the SSMS (SQL Server Management Studio). If not, try making a clean install of SQL Server.

Some more info:

  • http://www.ureader.com/msg/11614770.aspx
  • http://www.bigresource.com/Tracker/Track-ms_sql-UCk3FgDP/

Edit: I found that SQL Server Express works best if running a single default instance (the one named SQLEXPRESS), everything else gave me trouble, might just be me though.

like image 195
Sascha Hennig Avatar answered May 12 '26 16:05

Sascha Hennig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!