efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Object reference not set to an instance of an object.

Open 0x3ch0 opened this issue 6 years ago • 10 comments

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

0x3ch0 avatar Jan 03 '20 09:01 0x3ch0

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

ajcvickers avatar Jan 03 '20 17:01 ajcvickers

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

0x3ch0 avatar Jan 06 '20 13:01 0x3ch0

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

ajcvickers avatar Jan 06 '20 17:01 ajcvickers

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

WebApplication1.zip

0x3ch0 avatar Jan 07 '20 13:01 0x3ch0

@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 avatar Jan 08 '20 21:01 ajcvickers

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

0x3ch0 avatar Jan 09 '20 13:01 0x3ch0

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

ajcvickers avatar Jan 09 '20 16:01 ajcvickers

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.

roji avatar Jan 09 '20 17:01 roji

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!

0x3ch0 avatar Jan 10 '20 11:01 0x3ch0

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

Thanks!!!!!

lhernandez88 avatar Jan 10 '21 19:01 lhernandez88