Search code examples
c#.net-coreconnection-string

Keyword not supported: trusted_connection (Parameter 'keyword')


I have a .NET Core application. And normally I just can build the app with dotnet watch run.

But apparently something changed. Because if I do now dotnet run watch I get this error:

API.Program[0]
An error occurred during migration

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

So I checked my 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"
}

and my Program:

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>();
            });
}

I don't see what is wrong about these files.

Thank you

This is my configuration:

<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>

This is my 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;
    }
}

complete log:

 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>

Solution

  • You could try to change the Trusted_Connection/Trustedconnection to Integrated Security=True