1

I am trying to write application in ASP.NET, already created Models, Context and want to create DB with EF Code-First approach, but it does not work.

Additionally, folder App_Data is empty. I have installed SQL Server 2017 Express.

I tried every possible solution I could find, still, nothing worked.

Connection string:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-BM-20171207072135.mdf;Initial Catalog=aspnet-BM-20171207072135;Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

However, I get this error:

PM> update-database 

> Specify the '-Verbose' flag to view the SQL
> statements being applied to the target database

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50
Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start.

Errors:
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed) at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.b__32() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action1 act) at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act) at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:00000000-0000-0000-0000-000000000000

>

Error Number:-1983577846,State:0,Class:20
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Error occurred during LocalDB instance startup: SQL Server process failed to start)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sadixu
  • 15
  • 1
  • 3
  • While running that `update-database` in console, ensure that you're targeting the right `project`. You can also check in VS if you can connect to your `LocalDB` instance (that's what your connection string is using). – EdSF Dec 08 '17 at 00:09
  • Yea, working on right project (always checking "Default project" above console). How can I check this connection? Can't find any servers in VIEW->SERVER EXPLORER except "DESKTOP-KV92T89", but it doesn't have any databases – Sadixu Dec 08 '17 at 00:15
  • This [SO post](https://stackoverflow.com/questions/26248293/sql-network-interfaces-error-50-local-database-runtime-error-occurred-canno) goes through quite a bit trying to figure out `localdb` - version, installed, etc. (your conn string _looks_ ok though). In any case, since you can connect to that instance of SQL, why not use it for now. You can create a database and point to it. Comment out your conn string above, copy it and change accordingly (pointing to what looks like SQL Express). Make sure you retain the name `DefaultConnection` as thats configured in EF at the moment. – EdSF Dec 08 '17 at 00:46
  • Have you even **installed** SQL Server LocalDB ?? When you open a Windows command prompt - can you run `sqllocaldb versions` - and if so, what's your output?` – marc_s Dec 08 '17 at 06:19
  • It throws out `Windows API call "RegGetValueW" returned error code: 0.` Thought, that localdb will install itself automatically with sql express – Sadixu Dec 08 '17 at 10:00

1 Answers1

1

Make sure your sql server is up and running. Look from windows services (services.msc). Or to make sure, try to connect first manually using VS DB connection or SSMS.

Try to check this this (below) from your sql service properties too. enter image description here

Pete
  • 46
  • 3
  • That propably worked, mdf file appeared in App_Data folder and I got no errors when trying to update-database in PM console. Thank You very much! Additionally, I put in CMD: `sqllocaldb delete MSSQLLocalDB` and then `sqllocaldb create MSSQLLocalDB` Everything works like a charm. – Sadixu Dec 08 '17 at 10:12