AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Unable to update database using Entity Core Framework after migrations

Open adeisbright opened this issue 5 years ago • 14 comments

I tried updating my database after running migrations but got a System exception error. This is the command I used :

dotnet ef database update

This is the error message :

System.ArgumentException: Connection string keyword 'server' is not supported


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

adeisbright avatar Mar 13 '21 16:03 adeisbright

What environment? What version? What does the connection string look like.

The tutorial works. I'm not sure what step you missed. You might try asking on StackOverflow,.

Rick-Anderson avatar Mar 13 '21 17:03 Rick-Anderson

I am running the tutorial on Windows 10. This is a snapshot of the connection string : "Server=(localdb)\mssqllocaldb;Database=RazorPagesMovieContext-236d8ba3-1734-471d-a757-143be5b60c2f;Trusted_Connection=True;MultipleActiveResultSets=true"

adeisbright avatar Mar 13 '21 18:03 adeisbright

I will not say the tutorial is not working if everything worked fine. I checked Stackoverflow for answers before creating the issues here. All what it pointed was some error connected to ADO.NET but none linked to the SystemException that was thrown. I will gladly accept any help to fix the issue

adeisbright avatar Mar 13 '21 18:03 adeisbright

Exit/restart VS.

Rick-Anderson avatar Mar 14 '21 00:03 Rick-Anderson

The tutorial is mis-leading up to this point. The development setting is using options.UseSqlite in Startup.cs, but the connection string in appsettings.json is using SqlServer format.

change SqlServer format "Server=(localdb)\mssqllocaldb;Database=RazorPagesMovieContext-236d8ba3-1734-471d-a757-143be5b60c2f;Trusted_Connection=True;MultipleActiveResultSets=true"

To SqlLite format: "ConnectionStrings": { "RazorPagesMovieContext": "Data Source=MvcMovie.db" }

procome avatar Apr 03 '21 02:04 procome

@wadepickett please review

Rick-Anderson avatar Apr 03 '21 03:04 Rick-Anderson

The tutorial is mis-leading up to this point. The development setting is using options.UseSqlite in Startup.cs, but the connection string in appsettings.json is using SqlServer format.

When you scaffold for SQLite, it generates the SQLite connection string.

Rick-Anderson avatar Apr 03 '21 03:04 Rick-Anderson

No idea. Follow the step of the tutorial,

https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-5.0&tabs=visual-studio-code

dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

Adding this if (Environment.IsDevelopment()) { services.AddDbContext(options => options.UseSqlite( Configuration.GetConnectionString("RazorPagesMovieContext"))); } else { services.AddDbContext(options => options.UseSqlServer( Configuration.GetConnectionString("MovieContext"))); }

Then run dotnet ef migrations add InitialCreate dotnet ef database update

may duplicate the problem.

procome avatar Apr 03 '21 04:04 procome

Use SQLite for development, SQL Server for production is exactly that, for deploying to production, not for this tutorial.

Rick-Anderson avatar Apr 03 '21 04:04 Rick-Anderson

@wadepickett dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries

Why are we using -dc RazorPagesMovieContext ? I think that's used when you specify the db context. In this case we want the scaffolder to generate it.

Rick-Anderson avatar Apr 03 '21 04:04 Rick-Anderson

@wadepickett you're also missing --useSqlite|-sqlite : Flag to specify if DbContext should use SQLite instead of SQL Server.

This is a pri 0 bug.

Rick-Anderson avatar Apr 03 '21 04:04 Rick-Anderson

I know this framework two years ago. But I found no way to learn this framework at that time. Today, I try to learn it again. And I already stucked at part two of the first tutorial due to this problem. Why can the .NET ecosystem have a completable and enlightenment course? Like Laracasts from Laravel ecosystem.

tsewaiho avatar Apr 04 '21 17:04 tsewaiho

@Rick-Anderson, I am on this one now.

You asked: "Why are we using -dc RazorPagesMovieContext ?"

I don't know actually, sorry. You had -dc RazorPagesMovieContext in your topic for your updates to Model.md from 2018 to 2021.

2018-12 Update model.md (#9947) dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

2020-1 Update model.md by Rick-Anderson · Pull Request #16588 dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages\Movies --referenceScriptLibraries

2021-4 Update model.md (#22029) dotnet aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries -sqlite

2021-5 Update to Model.md 22367 dotnet aspnet-codegenerator changed to dotnet-aspnet-codegenerator and still has -dc RazorPagesMovieContext. dotnet-aspnet-codegenerator razorpage -m Movie -dc RazorPagesMovieContext -udl -outDir Pages/Movies --referenceScriptLibraries -sqlite

-dc or no -dc:

On (#22029) you originally tried the following without -dc, but it didn’t work: dotnet aspnet-codegenerator razorpage -m Movie -udl -outDir Pages/Movies --referenceScriptLibraries -sqlite … so you put the -dc RazorPagesMovieContext back in. That is why it is there.

At the time you asked deepchoudhery for verification but I don't think we got a reply, he may have been out.

I am honestly not sure what the proper command is supposed to be for this case. There is very little info available. It seems like the scaffolding isn't working how we expect it to, I don't know how to get it to generate on its own if the dc is not specified in the command.

If you know exactly what it should be, please let me know. If not, I will check with the developer on the intent vs. how it works now. The command reference may also need to be updated. Thanks for all the help.

wadepickett avatar Aug 18 '21 19:08 wadepickett

Instructions work fine as they are.

Rick-Anderson avatar Aug 19 '21 01:08 Rick-Anderson