Closed staff0rd closed 8 years ago
I am not able to reproduce this issue. Here is the code I used against 7.0.0-rc1-final. If you are still able to reproduce the issue, then feel free to re-open.
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Identity;Trusted_Connection=True;"));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddLogging();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
var serviceProvider = services.BuildServiceProvider();
var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
Setup(context, userManager, roleManager).Wait();
SetRole(userManager).Wait();
}
private async static Task Setup(ApplicationDbContext context, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
context.Database.EnsureCreated();
if (!userManager.Users.Any())
{
await userManager.CreateAsync(new ApplicationUser { UserName = "myUser", Email = "myUser@sample.com" });
}
if (!roleManager.Roles.Any())
{
await roleManager.CreateAsync(new IdentityRole("myRole"));
}
}
private async static Task SetRole(UserManager<ApplicationUser> userManager)
{
try
{
var user = await userManager.FindByNameAsync("myUser"); // successful
await userManager.AddToRoleAsync(user, "myRole"); // exception
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
}
public class ApplicationUser : IdentityUser
{
}
}
I'm assuming MARS is no longer a requirement due to PR #2198 and #1665, but the following snippet results in
UserManager.AddToRoleAsync
throwingSystem.InvalidOperationException: Operation is not valid due to the current state of the object.
when MultipleActiveResultSets=false.CODE
OUTPUT