【问题标题】:Failing to update database schema using Entity Framework CLI无法使用实体框架 CLI 更新数据库架构
【发布时间】:2022-09-26 17:44:28
【问题描述】:

我使用 .NET CLI 在我的机器上创建了一个空的 dotnet new webapi 项目。我在appsettings.json 中设置了ApplicationDbContext 以及一个连接字符串,并创建了一个我想在数据库中创建的模型。

项目成功构建并运行命令时

dotnet ef migrations add InitialCreate

它会生成 Migrations 文件夹,其中包含迁移脚本。

运行脚本并使用dotnet ef database update 更新数据库时会出现问题,它会引发以下错误:

Entity Framework Core 6.0.6 使用提供者 \'Microsoft.EntityFrameworkCore.SqlServer:6.0.6\' 初始化 \'ApplicationDbContext\' 并带有选项:无 ...
...
...
ClientConnectionId:#@#@#@@-####-####-@###-#@#@#@#@
错误号:18456,状态:1,类:14
用户 \'abc###\\Guest\' 登录失败。

我不确定它为什么会抛出此错误以及如何修复它。

appsettings.json:

{
  \"Logging\": {
    \"LogLevel\": {
      \"Default\": \"Information\",
      \"Microsoft.AspNetCore\": \"Warning\"
    }
  },
  \"AllowedHosts\": \"*\",
  \"ConnectionStrings\": {
    \"DefaultConnection\": \"Server=localhost;Database=BookClub;Trusted_Connection=True;\"
  }
}

BoockClub.csproj:

<Project Sdk=\"Microsoft.NET.Sdk.Web\">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include=\"Dapper\" Version=\"2.0.123\" />
    <PackageReference Include=\"Microsoft.EntityFrameworkCore\" Version=\"6.0.6\" />
    <PackageReference Include=\"Microsoft.EntityFrameworkCore.Design\" Version=\"6.0.6\">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include=\"Microsoft.EntityFrameworkCore.SqlServer\" Version=\"6.0.6\" />
    <PackageReference Include=\"Microsoft.EntityFrameworkCore.Tools\" Version=\"6.0.6\">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include=\"System.Data.SqlClient\" Version=\"4.8.3\" />
  </ItemGroup>
</Project>

ApplicationDbContext.cs:

using BookClub.Models;
using Microsoft.EntityFrameworkCore;

namespace BookClub.Data;

public class ApplicationDbContext : DbContext
{
  public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
  {
  }

  public DbSet<Category> Categories { get; set; }
}

CategoryController.cs:

using BookClub.Data;
using Microsoft.AspNetCore.Mvc;

namespace BookClub.Controllers;

public class CategoryController : Controller
{
  private readonly ApplicationDbContext _db;

  public CategoryController(ApplicationDbContext db)
  {
    _db = db;
  }

  public IActionResult Index()
  {
    var categoryList = _db.Categories.ToList();
    return View();
  }
}

系统:

  • macOS 12.2.1

应用:

  • VS 代码
  • Docker - SQL Server
  • 用于 VS 代码的 SQL Server 扩展

    标签: c# .net sql-server entity-framework dotnet-cli


    【解决方案1】:

    问题是您的连接字符串。

    Trusted_Connection=True; 表示您的应用程序正在使用 Windows 凭据进行身份验证,除非您在 Docker 中运行具有所有必要配置的 Windows,否则它将无法正常工作。

    您需要创建一个 SQL Authenticated DB 用户(使用用户名和密码),然后更改连接字符串

    旧值

    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=BookClub;Trusted_Connection=True;"
      }
    

    新价值

    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=BookClub;User Id=MyUsername;Password=MyPassword;"
      }
    

    如果您使用的是 SQL Server Management Studio,请使用以下选项来创建用户名和密码:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2011-03-10
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    相关资源
    最近更新 更多