不支持关键字:trusted_connection(参数“关键字”)

mig*_*ton 2 c# connection-string .net-core

我有一个 .NET Core 应用程序。通常我只需使用dotnet watch run.

但显然有些事情发生了变化。因为如果我现在这样做dotnet run watch,我会收到此错误:

API.Program[0]
迁移期间发生错误

System.ArgumentException:不支持关键字:trusted_connection(参数“关键字”)

在 Npgsql.NpgsqlConnectionStringBuilder.GetProperty(字符串关键字)
在 Npgsql.NpgsqlConnectionStringBuilder.set_Item(字符串关键字,对象值)
在 System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(字符串值)
在 Npgsql.NpgsqlConnectionStringBuilder..ctor(字符串连接字符串)
在 Npgsql .EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.ExistsAsync(CancellationToken CancellationToken)
在 Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken CancellationToken)
在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken CancellationToken)
在E:\Mijn Documents\UDEMY\C#\Reactivities-main\api\Program.cs 中的 API.Program.Main(String[] args):第 28 行

所以我检查了我的appsettings.development.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-EIC4DNM; Initial Catalog=reactivities;Trusted_Connection=true;"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "TokenKey": "super secret key"
}
Run Code Online (Sandbox Code Playgroud)

和我的程序:

public class Program
{
    public static async Task Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();

        using var scope = host.Services.CreateScope();

        var services = scope.ServiceProvider;
        var seed = new Seed();

        try
        {
            var context = services.GetRequiredService<DataContext>();
            var userManager = services.GetRequiredService<UserManager<AppUser>>();
            await context.Database.MigrateAsync();
            await seed.SeedData(context, userManager);
        }
        catch (Exception ex)
        {
            var logger = services.GetRequiredService<ILogger<Program>>();
            logger.LogError(ex, "An error occurred during migration");
        }

        await host.RunAsync();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
Run Code Online (Sandbox Code Playgroud)

我不明白这些文件有什么问题。

谢谢

这是我的配置:

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentValidation.AspNetCore" Version="10.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Application\Application.csproj" />
    <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

这是我的startupExtensionClass:

public static class StartupExtensionClass
{       
    //public System.Type type; 

    public static IServiceCollection AddApplicationServices(this IServiceCollection services,
        IConfiguration Configuration)
    {
        services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
            });

        services.AddDbContext<DataContext>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddCors(opt =>
        {
            opt.AddPolicy("CorsPolicy", policy =>
            {
                 policy.AllowAnyMethod().AllowAnyHeader().WithOrigins("http://localhost:3000");
            });
        });

        services.AddMediatR(typeof(List.Handler).Assembly);
        services.AddAutoMapper(typeof(MappingProfiles).Assembly);
        services.AddScoped<IUserAccessor, UserAccessor >();
        services.AddScoped<IPhotoAccessor, PhotoAccessor>();
        //services.AddHttpClient();
        services.Configure<CloudinarySettings>(Configuration.GetSection("Cloudinary"));

        return services;
    }
}
Run Code Online (Sandbox Code Playgroud)

完整日志:

 API.Program[0]
      An error occured during migraiton
      System.ArgumentException: Keyword not supported: trusted_connection (Parameter 'keyword')
         at Npgsql.NpgsqlConnectionStringBuilder.GetProperty(String keyword)
         at Npgsql.NpgsqlConnectionStringBuilder.set_Item(String keyword, Object value)
         at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
         at Npgsql.NpgsqlConnectionStringBuilder..ctor(String connectionString)
         at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.ExistsAsync(CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.ExistsAsync(CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
         at API.Program.Main(String[] args) in E:\Mijn Documents\UDEMY\C#\Reactivities-main\api\Program.cs:line 28
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: E:\Mijn Documents\UDEMY\C#\Reactivities-main\api
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
watch : Shutdown requested. Press Ctrl+C again to force exit.
watch : Exited
PS E:\Mijn Documents\UDEMY\C#\Reactivities-main\api>
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以尝试将 Trusted_Connection/Trustedconnection 更改为 Integrated Security=True