.Net Standard 2.0 中的 ApplicationDbContext.OnModelCreating() 有 MissingMethodException
ApplicationDbContext.OnModelCreating() in .Net Standard 2.0 has MissingMethodException
这是我在中发布的问题之一,但我想用一个小例子单独说明一下:
设置:
- .Net Standard 2.0 库
- .Net Framework 4.7.2 WebForms 应用程序
.Net 标准库有这个代码:
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 项目中:
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.EntityTypeBuilder1.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 项目。
所以我的问题是否有人知道以不同方式生成 Usermanager 的解决方法...我需要使用 aspnetcore 获取和设置用户(并验证)-身份(数据库等已经迁移)
所以,我有解决问题的方法:
使用 Microsoft.EntityFrameworkCore.SqlServer 版本 2.2.6(不是 3.1.1,尽管它应该与 .net 标准 2.0 一起工作...)
以及一系列
- 正在删除 nuget 包
- 正在清除 nuget 缓存
- 正在删除本地计算机上的 nuget 包(在 C:/../Users/.../.nuget 文件夹中)
- 将 nuget 管理格式更改为 "PackageReference"
- 哭得很厉害
- 重新安装所有内容并处理警告有帮助
(我不能说关于版本冲突的问题到底是什么)
这是我在
设置:
- .Net Standard 2.0 库
- .Net Framework 4.7.2 WebForms 应用程序
.Net 标准库有这个代码:
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 项目中:
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.EntityTypeBuilder1.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 项目。
所以我的问题是否有人知道以不同方式生成 Usermanager 的解决方法...我需要使用 aspnetcore 获取和设置用户(并验证)-身份(数据库等已经迁移)
所以,我有解决问题的方法:
使用 Microsoft.EntityFrameworkCore.SqlServer 版本 2.2.6(不是 3.1.1,尽管它应该与 .net 标准 2.0 一起工作...)
以及一系列
- 正在删除 nuget 包
- 正在清除 nuget 缓存
- 正在删除本地计算机上的 nuget 包(在 C:/../Users/.../.nuget 文件夹中)
- 将 nuget 管理格式更改为 "PackageReference"
- 哭得很厉害
- 重新安装所有内容并处理警告有帮助
(我不能说关于版本冲突的问题到底是什么)