Efcore: Object reference not set to an instance of an object.

Created on 3 Jan 2020  路  9Comments  路  Source: dotnet/efcore


I created a project with ASP.NET Core 3.1 Razor and added the Authentication.

After the project is created I installed the npgsql for Core 3.1 and change my json connection string.

I run the Update-Database command.

And the error shows

Object reference not set to an instance of an object.

Tried to delete the Migrations folder and add a new migration using

Add-Migration InitialMigration

And the error shows

No mapping to a relational type can be found for property 'Microsoft.AspNetCore.Identity.IdentityUser.TwoFactorEnabled' with the CLR type 'bool'.

So I think this is a bug

EF Core version: 3.1
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.4.1

closed-question customer-reported

Most helpful comment

@xshiiiidev I am not able to reproduce this. Add-Migration on that project works for me:

PM> Add-Migration InitialEntityMigration
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> 

However, I think I might understand part of the problem. The ASP.NET Core template comes with a pre-generated migration for SQL Server. If you then change the provider to Npgsql you will need to delete this migration and generate a new one. That may be why Update-Database failed. However, that doesn't explain why Add-Migration is not working after deleting the existing migrations.

All 9 comments

@xshiiiidev Please post a small project that reproduces this behavior so that we can investigate.

Hello @ajcvickers

You can follow what I do.

I created a new ASP.NET Core Razor 3.1 Project with Individual Authentication

image

Restored nuget package and then installing npgsql for .net core 3.1

image

Then I changed my connection string pointing to my PostgreSQL Database Local

image

After that I configure the Service to use UseNpgsql

image

Then I executed the code

Update-Database

And get the error below

image

The fix I tried was to delete the Migrations folder and re-create it using

Add-Migration InitialIdentityMigration

and this below error was shown

image

I am stuck here right now

@xshiiiidev Now that you have created the project can you attach it here so we can take a look?

Hi @ajcvickers sorry here is the source code of that project.

WebApplication1.zip

@xshiiiidev I am not able to reproduce this. Add-Migration on that project works for me:

PM> Add-Migration InitialEntityMigration
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> 

However, I think I might understand part of the problem. The ASP.NET Core template comes with a pre-generated migration for SQL Server. If you then change the provider to Npgsql you will need to delete this migration and generate a new one. That may be why Update-Database failed. However, that doesn't explain why Add-Migration is not working after deleting the existing migrations.

@ajcvickers I am able to proceed by changing my database provider to MySQL Pomelo without any issues testing this steps.

But I really want to use PostgreSQL than MySQL.

@xshiiiidev There should be no difference here between MySQL and PostgreSQL. The solution you attach targets PostgreSQL and the migrations are generated correctly.

@roji Any ideas?

The problem of the default migrations for SQL Server in the ASP.NET template projects is unfortunately well-known... As @ajcvickers wrote you have to remove them.

However, like @ajcvickers I'm unable to see an issue creating the migrations. I downloaded your ZIP above, unzipped it and ran dotnet ef migrations add initial, and Npgsql migrations were created without an error. Can you please give that a try again?

Also, note that your project continues to reference the SQL Server provider although you don't seem to want to use it. Better remove it to avoid confusion.

I already fixed the problem.

The problem was not efcore maybe, because I only updated my PostgreSQL server 12.0 to 12.1 and everything works fine from adding migration to updating database.

Thanks for the effort to help @ajcvickers

Regards to efcore team!

Was this page helpful?
0 / 5 - 0 ratings