Why is [Owin] throwing a null exception on new project?

前端 未结 16 1510
孤城傲影
孤城傲影 2020-12-25 10:01

I have a rather strange issue i\'m not sure how to fix or if i can even fix it.

I\'ve done some research into the issue but can\'t find an answer to what\'s causing

16条回答
  •  眼角桃花
    2020-12-25 10:34

    The reason this exception is probably being thrown is because there is a problem creating your ApplicationDbContext.

    In my case I added Migrations, and set

            Database.SetInitializer(new MigrateDatabaseToLatestVersion());
    

    And I started getting the same error as you.

    Turned out that when I tried to access any object in the database, the DbContext was throwing an error, saying AspNetUsers already exists as previously I have run my code without migrations enabled, therefore the Database was created, with all the correct tables needed for Identity, and once I did EnableMigrations, and set an initialiser, it was throwing an error saying that the Table already exists.

    In your case, my guess would be there is some underlying issue with the ApplicationDbContext, before the Startup, try the following before Config.Auth methods get called:

            var ctx = new ApplicationDbContext(...);
            var count = ctx.Users.Count();
    

    See if it returns the count or throws an exception.

提交回复
热议问题