1

Ok, here's my issue. I'm following the tutorial here:

https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-2.2&tabs=visual-studio

And when I get to the section where you first test the app, I get a connection error for my database. I haven't touched the connection string (the tutorial doesn't say to). But the error I get is this (through the console):

fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (76ms) [Parameters=[], CommandType='Text', CommandTimeout='60'] CREATE DATABASE [SchoolContext-ce3ad5d4-bcc1-45d9-a13f-50c3b2624a6f]; System.Data.SqlClient.SqlException (0x80131904): CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Users\SchoolContext-ce3ad5d4-bcc1-45d9-a13f-50c3b2624a6f.mdf'. CREATE DATABASE failed. Some file names listed could not be created. Check related errors. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) ClientConnectionId:2aa20854-bffb-4fcb-b736-4d1669245bf5 Error Number:5123,State:1,Class:16 fail: MY_PROJECT.Program[0] An error occurred creating the DB. System.Data.SqlClient.SqlException (0x80131904): CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Users\SchoolContext-ce3ad5d4-bcc1-45d9-a13f-50c3b2624a6f.mdf'. CREATE DATABASE failed. Some file names listed could not be created. Check related errors. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Create() at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() at MY_PROJECT.Program.Main(String[] args) in C:\Users\\source\repos\MY_PROJECT\MY_PROJECT\Program.cs:line 23 ClientConnectionId:2aa20854-bffb-4fcb-b736-4d1669245bf5 Error Number:5123,State:1,Class:16

So it's not even creating the database.

What could the issue be? I've Googled a few things, but nothing really seems to answer this specific question. Do I need to set permissions on the "my_username" folder in order for this to work?

4
  • You're trying to write directly to C:\Users, where you don't have write access. Your access starts at C:\Users\YourOwnUserName or C:\Users\Public. You have to change the connection string to point to a location where your account has permissions to write files before the DB can be created there. Commented Sep 16, 2019 at 1:15
  • Ok, here's my connection string: Server=(localdb)\\mssqllocaldb;Database=SchoolContext-ce3ad5d4-bcc1-45d9-a13f-50c3b2624a6f;Trusted_Connection=True;MultipleActiveResultSets=true" What do I need to add to do that? Commented Sep 16, 2019 at 1:26
  • First off, try running Visual Studio as Administrator and run it again. I don't expect that to work, but it's worth a shot. Most likely, the account that's running the Sql Server process doesn't have permissions to create a file in your user directory. You can change that configuration in Sql Server. techyaz.com/sql-server/… Commented Sep 16, 2019 at 2:30
  • Tried that. I'm still completely lost as to where to change it. Tried your link also. No dice there, either. Commented Sep 27, 2019 at 23:54

4 Answers 4

3

for me it was that => my appsettings.json and appsettings.Developement.json files not having the same connectionstring ... so go to both files and make sure they are containing the same databaseName.

Sign up to request clarification or add additional context in comments.

Comments

0

For anyone interested, I discovered the answer to this question.

Apparently, it's not a flaw in .NET or .NET Core. It's a SQL Server local DB issue. If you're running SQL 2017 (as I am) you have to install the latest cumulative update. that will fix the issue.

As of this time, the most recent cumulative update is this one: Cumulative Update Package 19 for SQL Server 2017 - KB4535007

Which I got from here: https://www.microsoft.com/en-us/download/details.aspx?id=56128

1 Comment

Could you update the answer to include more details to help people coming across this question to know which "cumulative update" you are referring to? The "latest cumulative update" probably won't stay that way.
0

Try the command from cmd. Do remember to run cmd as administrator. Let me know if this works!

netsh advfirewall firewall add rule name="SQL Server" dir=in action=allow protocol=TCP localport=1433

Comments

-1

Create a new maigration. in package manager console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.