public async Task<IdentityResult> AddToRole(AddToRoleViewModel userModel = null, string role = null,
UserViewModel userViewModel = null)
{
if (userModel != null)
{
var user = await _userManager.FindByEmailAsync(userModel.Email);
return await _userManager.AddToRoleAsync(user, userModel.RoleName);
}
else return await _userManager.AddToRoleAsync(userViewModel, role);
}
services.AddDbContext<ApplicationContext>(
options => {
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("Libraly.Data")
);
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
)
public class RegisterViewModel
{
//обязательные поля
[Required(ErrorMessage = "Пустое поле")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Пустое поле")]
public string FullName { get; set; }
}
RegisterViewModel fakeUser = new RegisterViewModel();
fakeUser.Password = null;
fakeUser.UserName = null;
fakeUser.Email = null;
public void CheckAddUser()
{
SetingUsers();
var moc = new Mock<IUserService>();
moc.Setup(c => c.Create(It.IsAny<RegisterViewModel>()) );
var userController = new TestController(moc.Object);
var result = userController.Create(fakeUser);
//должен вернуть ошибку
}
public async Task<ActionResult> Create(RegisterViewModel user)
{
// ModelState.AddModelError("swe","ssssss");
if (ModelState.IsValid)
{
var result = await _userService.Create(user);
return Ok(user);
}
else
{
return BadRequest(ModelState);
}
}
public class ApplicationContextFactory:IDesignTimeDbContextFactory
{
public ApplicationContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseNpgsql(b => b.MigrationsAssembly("Libraly.Data"));
return new ApplicationContext(optionsBuilder.Options);
}
}