ApplicationDbContext.OnModelCreating() in .Net Standard 2.0 has MissingMethodException

一世执手 提交于 2020-02-06 08:01:11

问题


this is one of the problems that I issued in "Found conflicts between different versions" OR System.MissingMethodException when using aspnetcore.identity, but I wanted to explain it separately with a small example:

SetUp:

  1. A .Net Standard 2.0 Library
  2. A .Net Framework 4.7.2 WebForms application

The .Net standard Library has this Code:

 public class Class1
    {
        public static void doStuff()
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer("MyConnectionString");

            var userStore = new UserStore<ApplicationUser, MyRole, ApplicationDbContext, Guid>(
                new ApplicationDbContext(optionsBuilder.Options)
                ); // the userStore.Users property shows already the exception in the debugger

            AspNetUserManager<ApplicationUser> userManager = new AspNetUserManager<ApplicationUser>(
                userStore,
                null,
                new PasswordHasher<ApplicationUser>(),
                new List<UserValidator<ApplicationUser>>() { new UserValidator<ApplicationUser>() },
                null,
                null,
                null,
                null,
                null
                );

            var x = userManager.Users; // exception throw (see next code snipped)
        }
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, MyRole, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder); // after userManager.Users is called the exception is thrown here
        }
    }

And in the WebForms-Project just:

public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Class1.doStuff();
        }
    }

Exception: System.MissingMethodException: 'Methode nicht gefunden: "Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder1.HasIndex(System.Linq.Expressions.Expression1>)".'

Configuration of .Net Standard 2.0 Project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.1" />
  </ItemGroup>

</Project>

References: https://github.com/dotnet/aspnetcore/issues/8467 Here they say to update to "Microsoft.AspNetCore.Identity" 3.x, but I cannot do that because its not compatible with the .Net Standard 2.0 project and I need that for the WebForms Project.

So my question would be if someone knows a workaround to generate an Usermanager in a different way... I need to Get and Set Users (and validate) using aspnetcore-identity (database etc. is already migrated)


回答1:


So, I have a solution for my problem:

use Microsoft.EntityFrameworkCore.SqlServer version 2.2.6 (NOT 3.1.1 even though its supposed to work with .net standard 2.0...)

And a sequence of

  • deleting nuget packages
  • clearing nuget cache
  • deleting nuget packages on the local computer (in the C:/../Users/.../.nuget folder)
  • changing the nuget management format to "PackageReference"
  • cry a lot
  • reinstalling everything and dealing with warnings helped

(I cant say what exactly the issue was regarding the version-conflicts)



来源:https://stackoverflow.com/questions/59988629/applicationdbcontext-onmodelcreating-in-net-standard-2-0-has-missingmethodexc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!