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

I am trying to create an embedded SQL Server Express database (.mdf) with a code-based Entity Framework. It worked fine, but now I just go over to the same error and I can not determine the problem. The problem occurs during initialization, it just freezes forever, and if you look at intellitrace, you can see some kind of loop of throw / catch statements that return

“Unable to open the physical file“ c: \ gt \ aspnetdb.mdf. ”System error 2:“ 2 (could not get the text for this error Cause: 1815. ”Cannot attach the file“ c: \ gt \ aspnetdb.mdf ”to as the database 'WikDb'. "(System.Data.SqlClient.SqlException)

UPDATE I also get the following error now before "inability to open a physical file ..."

The column "InvariantName" is limited to unique. The cost of '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 initialization code

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

Any help appreciated

Thanks,

+4
source share
1 answer

It seems your instance of SQL Server is corrupted. This tends to occur when installing additional instances quite often - if you use the Express version, that is, the full version is simplified for installation and maintenance (if you think that Google - or my personal experience: P). The best way to check if this can be done is to run the application on another machine with a working instance of SQL Server. You can connect to your database using SSMS (SQL Server Management Studio). If not, try a clean installation of SQL Server.

Additional Information:

Edit: I found that SQL Server Express works best if it starts one instance by default (one called SQLEXPRESS), everything else gave me problems, maybe only me.

+2
source

Source: https://habr.com/ru/post/1384944/


All Articles