mag*_*gos 50 c# asp.net asp.net-mvc entity-framework asp.net-identity
我在我的应用程序中使用ASP.NET身份时出错.
不支持每种类型的多个对象集.对象集"Identity Users"和"Users"都可以包含"Recommendation Platform.Models.ApplicationUser"类型的实例.
我在StackOverflow中看到了一些关于这个错误的问题.全部表示两个DbSet
相同类型的对象.但在我看来DbContext
有不一样的类型DbSets
.FindAsync()
在登录期间抛出方法的异常.
if (ModelState.IsValid)
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null && user.IsConfirmed)
{
Run Code Online (Sandbox Code Playgroud)
问题是我没有两个DbSets
相同的类型.我的上下文看起来像这样:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public System.Data.Entity.DbSet<RecommendationPlatform.Models.ApplicationUser> IdentityUsers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class RecContext : DbContext
{
public RecContext()
: base("RecConnection")
{
Database.SetInitializer<RecContext>(new DropCreateDatabaseIfModelChanges<RecContext>());
}
public DbSet<Recommendation> Recommendations { get; set; }
public DbSet<Geolocation> Geolocations { get; set; }
public DbSet<Faq> Faqs { get; set; }
public DbSet<IndexText> IndexTexts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
什么可能导致这个问题?也许与内置的ASP.NET身份功能相关的东西?无论如何,什么是Users
类型?我的应用程序中没有它...
Mar*_*zek 100
你有两个DbSet
相同类型的s`.
IdentityDbContext<T>
本身包含Users
声明为的属性:
public DbSet<T> Users { get; set; }
Run Code Online (Sandbox Code Playgroud)
你在课堂上宣布第二个.
小智 70
查看这个文件"ApplicationDbContext.cs",删除最后由scaffold生成的行,应该是这样的:
public System.Data.Entity.DbSet<Manager.Models.ApplicationUser> IdentityUsers { get; set; }
Run Code Online (Sandbox Code Playgroud)
此问题可能是由于使用脚手架来创建支架而引起的View
。您可能执行了以下操作:视图>添加>新脚手架项目...> MVC 5视图> [模型类:ApplicationUser]。
脚手架向导在您的ApplicationDbContext
类中添加了一行新代码。
public System.Data.Entity.DbSet<RecommendationPlatform.Models.ApplicationUser> IdentityUsers { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在,您具有两个DbSet
相同类型的属性,这不仅会导致FindAsync()
方法中抛出异常,而且还会在您尝试使用代码优先迁移时引发异常。
使用脚手架时要非常小心,甚至最好不要使用它。
归档时间: |
|
查看次数: |
43089 次 |
最近记录: |