Reputation:
Well I'm following this tutorial that was suggested by my teacher to understand the work with roles Roles
The problem is I did everything step by step right but in the last step I can’t get it working because it says: Default parameter 'value' for context must be a compile time constant
I tried everything and I don’t know why it does not work. If someone can give a hint to solve this I would appreciate a lot.
Home Controller
using (var context = new ApplicationDbContext())
{
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.Create(new IdentityRole("Admin"));
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.FindByEmail("[email protected]");
userManager.AddToRole(user.Id, "Admin");
context.SaveChanges();
}
Upvotes: 0
Views: 80
Reputation: 149
I believe the problem is with var context = new ApplicationDbContext()
. C# expects default values to be "compile time constants," which includes things like number and string literals, and excludes new instances of classes or returns from method calls. The simplest way I can think of to pull off what you are trying is to replace the argument with ApplicationDbContext context = null
(null can be the default value because it is a compile time constant) and add in the line to the method body if(context == null){ context = new ApplicationDbContext(); }
Upvotes: 1