Trouble connecting to DB in .NET Core app while following tutorial

1

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

https://docs.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?

.net-core
asked on Stack Overflow Sep 16, 2019 by M1976

2 Answers

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

answered on Stack Overflow Mar 24, 2020 by M1976 • edited Mar 25, 2020 by M1976
0

Create a new maigration. in package manager console.

answered on Stack Overflow Oct 28, 2020 by Mohsen.bakhtiyariha

User contributions licensed under CC BY-SA 3.0