2

I have this ASP.NET Core project.

enter image description here

and the connection string that points to my SQL database.

 "ConnectionStrings": {
    "DefaultConnection": "Data Source=localhost;Initial Catalog=RestaurantDatabase;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true"}

I need to initialize the database, thus run migration script. I've tried with update-database but that did not work. What is the first step when initializing database from code?

UPDATE:

PM> dotnet ef migrations add InitialCreate
dotnet : No executable found matching command "dotnet-ef"
At line:1 char:1
+ dotnet ef migrations add InitialCreate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (No executable f...and "dotnet-ef":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PM> Install-Package Microsoft.EntityFrameworkCore.Tools
  CACHE https://api.nuget.org/v3/registration3-gz-semver2/microsoft.entityframeworkcore.tools/index.json
Restoring packages for E:\xxxx\Restaurant-App\src\Server\Restaurant.Server.Api\Restaurant.Server.Api.csproj...
Install-Package : NU1605: Detected package downgrade: Microsoft.EntityFrameworkCore.Design from 2.0.2 to 2.0.1. Reference the package directly from the project to select a 
different version. 
 Restaurant.Server.Api -> Microsoft.EntityFrameworkCore.Tools 2.0.2 -> Microsoft.EntityFrameworkCore.Design (>= 2.0.2) 
 Restaurant.Server.Api -> Microsoft.EntityFrameworkCore.Design (>= 2.0.1)
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Install-Package : Package restore failed. Rolling back package changes for 'Restaurant.Server.Api'.
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore.Tools
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand


PM> Add-Migration myMigration
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\xxxx\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
infoinfo: IdentityServer4.Startup[0]
      You are using the in-memory version of the persisted grant store. This will store consent decisions, authorization codes, refresh and reference tokens in memory only. If you are using any of those features in production, you want to switch to a different store implementation.
: IdentityServer4.Startup[0]
      You are using the in-memory version of the persisted grant store. This will store consent decisions, authorization codes, refresh and reference tokens in memory only. If you are using any of those features in production, you want to switch to a different store implementation.
dbug: IdentityServer4.Startup[0]
      Using Bearer as default scheme for authentication

Nuget packages Nuget packages

The script in question is this -> https://github.com/Jurabek/Restaurant-App

7
  • Do you receive an exception? Which one? Maybe this MS doc article - Migrations could help. Commented Apr 17, 2018 at 8:47
  • What do you mean by not work? Commented Apr 17, 2018 at 8:48
  • I tried to run migrations and then install EF tools but both commands produce error. Please see above. Commented Apr 17, 2018 at 9:05
  • 1
    use "Add-Migration InitialCreate" command more details -> learn.microsoft.com/en-us/ef/core/managing-schemas/migrations Commented Apr 17, 2018 at 9:08
  • No matter what I try I always get the exception. Please check the updated post. Commented Apr 17, 2018 at 11:13

2 Answers 2

1

NOTE: the Restaurant.Server.Api.csproj configured by DotNet command line not with Package Manager and also you don't need to create the initial migration.

you need to go to the src\Server\Restaurant.Server.Api>

run that commands with command line dotnet restore

and dotnet ef-database update

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

4 Comments

thanks for the reply Jurabek. I did exactly what you said, and now there is error -> pastebin.com/nyCN5ub7
Migration script could not create database object. Could you help me with this ? pastebin.com/3X7Z5Aeu
I think you don't have access to your connection string the application can not connecting to the database.
The connection string is just as usual: "Data Source=localhost;Initial Catalog=RestaurantDatabase;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true". The RestaurantDatabaseis already created on the server but its not populated by the script. that's why there is an error with "AspNetRoles" . any hints ?
0

You have to first generate Migration by running Add-Migration YourMigrarionName from Package Manager console in Visual Studio and then run Update-Database to run the Migration on DB.

3 Comments

No matter what I try I always get the exception. Please check the updated post.
which EF Core version do you have?
2.0.2. all the nuget packages are updated to latest version. (please check the attachment in original post)

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.