Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Connecting to databse? [EF Core] [npgsql/PostgreSQL] [Identity]

$
0
0

Hello all,

I've created a project through the wizard with the template ASP.NET Core Web Application, authentication: Individual user account.
I then installed Npgsql.EntityFrameworkCore.PostgreSQL.

Now im obviously trying to connect these. The PostgreSQL server is fine, and VS can connect to it through Server Explorer with the DevArt dotConnect I installed earlier.
By default Identity?/Entitity? uses MSSQL by default.

I couldn't find the answer on the internet, so I started to try things, and after awhile became very puzzled if I'm on the right track. Thanks!

I updated the the default connection string in appsettings.json:

"ConnectionStrings": {"DefaultConnection": "Server=localhost;Port=5432;User Id=postgres;Password=********;Database=SamenApp;Persist Security Info=True"
  },

and changed Startup.cs to load Npgsql:

        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        } 

When a user connects I get a exception at base(options)

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    ...

An exception of type 'System.TypeLoadException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

Additional information: Could not load type 'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>