Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core NET核心代码第一,数据库初始化_Asp.net Core_Ef Code First_Entity Framework Core_Entity Framework Migrations - Fatal编程技术网

Asp.net core NET核心代码第一,数据库初始化

Asp.net core NET核心代码第一,数据库初始化,asp.net-core,ef-code-first,entity-framework-core,entity-framework-migrations,Asp.net Core,Ef Code First,Entity Framework Core,Entity Framework Migrations,我有这个ASP.NET核心项目 以及指向我的SQL数据库的连接字符串 "ConnectionStrings": { "DefaultConnection": "Data Source=localhost;Initial Catalog=RestaurantDatabase;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true"} 我需要初始化数据库,从而运行迁移脚本。我尝试了更新

我有这个ASP.NET核心项目

以及指向我的SQL数据库的连接字符串

 "ConnectionStrings": {
    "DefaultConnection": "Data Source=localhost;Initial Catalog=RestaurantDatabase;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=true"}
我需要初始化数据库,从而运行迁移脚本。我尝试了
更新数据库
,但没有成功。 从代码初始化数据库的第一步是什么

更新:

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软件包


有问题的脚本是->

您必须首先从Visual Studio中的Package Manager控制台运行
添加迁移YourMigration YourMigration InName
来生成迁移,然后运行
Update Database
在DB上运行迁移。

注意: DotNet命令行配置的Restaurant.Server.Api.csproj不使用Package Manager,也不需要创建初始迁移

您需要转到src\Server\Restaurant.Server.Api>

使用命令行
dotnet restore运行该命令


dotnet ef数据库更新

您是否收到异常?哪一个?也许这会有帮助。你说不工作是什么意思?我试图运行迁移,然后安装EF工具,但这两个命令都会产生错误。请参见上文。使用“添加迁移初始创建”命令了解更多详细信息->无论我尝试什么,我总是会遇到异常。请检查更新后的帖子。无论我尝试什么,我总是得到例外。请检查更新后的帖子。你有哪个EF核心版本?2.0.2。所有nuget软件包都更新为最新版本。(请查看原始邮件中的附件)感谢Jurabek的回复。我完全按照你说的做了,现在出现了一个错误->迁移脚本无法创建数据库对象。你能帮我做这个吗?我认为您没有访问连接字符串的权限应用程序无法连接到数据库。连接字符串与往常一样:“数据源=本地主机;初始目录=RestaurantDatabase;集成安全性=True;受信任的连接=True;MultipleActiveResultSets=True”。RestaurantDatabases已在服务器上创建,但其未由脚本填充。这就是为什么“AspNetRoles”出现错误的原因。有什么提示吗?