ASP.NET MVC5:无法使用自定义db上下文添加新角色

时间:2016-02-14 20:10:02

标签: c# asp.net asp.net-mvc asp.net-identity argumentnullexception

我的ASP.NET MVC项目的自定义db上下文有一个非常恶心的问题。

我已经尝试将自动生成的ApplicationDbContext与我自己的GSDbContext合并,同时仍然保持IdentityUsersIdentityRoles正常工作。

这是我的自定义ApplicationUser类定义:

 public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstAndLastName { get; set; }

    public bool Active { get; set; }
}

上下文类:

public class GSDbContext : IdentityDbContext
{
     public GSDbContext()
          : base("GSDbContext")
     {
     }

     public virtual IDbSet<ApplicationUser> Users { get; set; }
     public virtual IDbSet<IdentityRole> Roles { get; set; }
     public virtual IDbSet<IdentityUserLogin> UserLogins { get; set; }
     public virtual IDbSet<IdentityUserClaim> UserClaims { get; set; }
     public virtual IDbSet<IdentityUserRole> UserRoles { get; set; }
}

现在发生的事情是,当我尝试添加新角色时,我得到System.ArgumentNullException。顺便说一句,我可以轻松创建一个没有血腥痛苦的新用户。我使用此代码创建一个新用户:

var userManager = new UserManager<ApplicationUser>(
            new UserStore<ApplicationUser>(new GSDbContext()));
userManager.Create(user, password);

添加新角色:

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new GSDbContext()));
roleManager.Create(new IdentityRole("Admin"));

引发以下异常:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: source
  Source=System.Core
  ParamName=source
  StackTrace:
       at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
       at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items)
       at System.Data.Entity.DbContext.GetValidationErrors()
       at System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken)
       at System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken)
       at System.Data.Entity.DbContext.SaveChangesAsync(CancellationToken cancellationToken)
       at System.Data.Entity.DbContext.SaveChangesAsync()
       at Microsoft.AspNet.Identity.EntityFramework.RoleStore`1.<CreateAsync>d__2.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at Microsoft.AspNet.Identity.RoleManager`1.<CreateAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
       at Microsoft.AspNet.Identity.RoleManagerExtensions.Create[TRole](RoleManager`1 manager, TRole role)

这让我非常疯狂。

提前致谢。

2 个答案:

答案 0 :(得分:0)

不要将DbContext包含的身份模型包含在IdentityDbContext中。所以你可以改变你的上下文:

public class GSDbContext : IdentityDbContext<ApplicationUser>
{
    // put your OTHER entities apart from identity related onces
    public IDbSet<MyModel> MyModels { get; set; }
    // and so on
}

答案 1 :(得分:0)

您应该删除IdentityDbContext中已存在的属性。

public class GSDbContext : IdentityDbContext
{
     public GSDbContext()
          : base("GSDbContext")
     {
     }
     public virtual IDbSet<ApplicationUser> Users { get; set; }
     //Nothing ELSE
}