.NET Standard 2.0中的ApplicationDbContext.OnModelCreating()具有MissingMethodException [英] ApplicationDbContext.OnModelCreating() in .Net Standard 2.0 has MissingMethodException
问题描述
这是我在中发布的问题之一 ;发现不同版本之间存在冲突。或使用aspnetcore.identity 时出现System.MissingMethodException,但我想用一个小例子单独解释一下:
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:
- .Net Standard 2.0库
- .Net Framework 4.7.2 WebForms应用程序
.Net标准库具有以下代码:
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
}
}
在WebForms-Project中只是:
And in the WebForms-Project just:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Class1.doStuff();
}
}
例外:
System.MissingMethodException:'Methode nicht gefunden: Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder 1.HasIndex(System.Linq.Expressions.Expression
1>)。'
.Net Standard 2.0项目的配置:
<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>
参考文献:
https://github.com/dotnet/aspnetcore/issues/8467 在这里,他们说要更新为 Microsoft.AspNetCore.Identity 3.x,但我不能这样做,因为它与.Net Standard 2.0项目不兼容,而WebForms项目则需要它。
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.
所以我的问题将是是否有人知道以其他方式生成Usermanager的解决方法...我需要使用aspnetcore-identity(数据库等已迁移,从而获取和设置用户(并验证)) )
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)
推荐答案
因此,我有一个解决方案:
So, I have a solution for my problem:
使用Microsoft.EntityFrameworkCore.SqlServer 2.2.6版(不 3.1.1,即使它应与.net standard 2.0兼容...)
use Microsoft.EntityFrameworkCore.SqlServer version 2.2.6 (NOT 3.1.1 even though its supposed to work with .net standard 2.0...)
和一系列
- 删除nuget程序包
- 清除nuge t缓存
- 删除本地计算机上的nuget包(在C:/../ Users /.../。nuget文件夹中)
- 将nuget管理格式更改为 PackageReference
- 哭了很多
- 重新安装所有内容并处理了警告,
- 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)
这篇关于.NET Standard 2.0中的ApplicationDbContext.OnModelCreating()具有MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!