Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC5标识用户管理器。更新(用户)不工作_C#_Entity Framework_Asp.net Mvc 5_Owin - Fatal编程技术网

C# MVC5标识用户管理器。更新(用户)不工作

C# MVC5标识用户管理器。更新(用户)不工作,c#,entity-framework,asp.net-mvc-5,owin,C#,Entity Framework,Asp.net Mvc 5,Owin,这是我第一次在MVC5中使用代码优先进行用户管理。我可以更改密码,但无法更新用户注册。我已经浏览了互联网,仔细查看了这个网站上与UserManager.Update(user)不起作用相关的所有问题 using System.Data.Entity; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity

这是我第一次在MVC5中使用代码优先进行用户管理。我可以更改密码,但无法更新用户注册。我已经浏览了互联网,仔细查看了这个网站上与UserManager.Update(user)不起作用相关的所有问题

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace _100..Models
{

    public class ApplicationUser : IdentityUser
    {
        public virtual PersonalInfo PersonalInfo { get; set; }
        public virtual BillingInfo BillingInfo { get; set; }
        public virtual DeliveryInfo DeliveryInfo { get; set; }
        public Chapters Chapter { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class PersonalInfo
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class BillingInfo
    {
        public int ID { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
    }

    public class DeliveryInfo
    {
        public int ID { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
    }


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false){}

        public DbSet<PersonalInfo> PersonalInfo { get; set; }
        public DbSet<BillingInfo> BillingInfo { get; set; }
        public DbSet<DeliveryInfo> DeliveryInfo { get; set; }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}
使用System.Data.Entity;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.EntityFramework;
名称空间_100..Models
{
公共类应用程序用户:IdentityUser
{
公共虚拟PersonalInfo PersonalInfo{get;set;}
公共虚拟BillingInfo BillingInfo{get;set;}
公共虚拟DeliveryInfo DeliveryInfo{get;set;}
公共章节章节{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共类个人信息
{
公共int ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
公共类计费信息
{
公共int ID{get;set;}
公共字符串AddressLine1{get;set;}
公共字符串AddressLine2{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zip{get;set;}
}
公共类传递信息
{
公共int ID{get;set;}
公共字符串AddressLine1{get;set;}
公共字符串AddressLine2{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zip{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false){}
公共DbSet PersonalInfo{get;set;}
公共数据库集BillingInfo{get;set;}
公共DbSet DeliveryInfo{get;set;}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}
}
这是我的行动

[HttpPost]
        public async Task<ActionResult> UpdateRegisteration(ApplicationUser user)
        {
            var result = await UserManager.UpdateAsync(user);
            return RedirectToAction("Index", "Home");
        }
[HttpPost]
公共异步任务更新注册(ApplicationUser用户)
{
var result=await UserManager.UpdateAsync(用户);
返回重定向到操作(“索引”、“主页”);
}

我已经确认,用户对象在点击操作时具有更新的数据,更新方法返回成功,但它实际上没有更新数据库。

此修复有几个层。首先,当我执行UserManager.Update时,它是针对我从updateRegistration视图收到的用户对象执行的。EF认为这是一个新对象,并引发内部错误“用户名已存在”,失败但报告成功。因此,我必须创建一个dbContext并将实体状态更新为modified。但我了解到,我还必须根据更新的用户ID创建一个用户对象来设置用户名,否则更新失败。我发现我必须更新PasswordHash,否则它将为null。然后我发现我还必须更新SecurityStamp,否则登录过程会出错。我的行动如下

 [HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser UpdatedUser)
{
    var SavedUser = await UserManager.FindByIdAsync(UpdatedUser.Id);

    try
    {
        UpdatedUser.SecurityStamp = SavedUser.SecurityStamp;
        UpdatedUser.PasswordHash = SavedUser.PasswordHash;
        UpdatedUser.UserName = SavedUser.UserName;
        UpdatedUser.Id = SavedUser.Id;
        UpdatedUser.PersonalInfo.ID = SavedUser.PersonalInfo.ID;
        UpdatedUser.BillingInfo.ID = SavedUser.BillingInfo.ID;
        UpdatedUser.DeliveryInfo.ID = SavedUser.DeliveryInfo.ID;

        ApplicationDbContext db = new ApplicationDbContext();
        db.Entry(UpdatedUser).State = EntityState.Modified;
        db.Entry(UpdatedUser.PersonalInfo).State = EntityState.Modified;
        db.Entry(UpdatedUser.BillingInfo).State = EntityState.Modified;
        db.Entry(UpdatedUser.DeliveryInfo).State = EntityState.Modified;
        await db.SaveChangesAsync();

        //            var result = await UserManager.UpdateAsync(SavedUser);
        return RedirectToAction("Index", "Home");
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    {
        Exception raise = dbEx;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                string message = string.Format("{0}:{1}",
                    validationErrors.Entry.Entity.ToString(),
                    validationError.ErrorMessage);
                // raise a new exception nesting
                // the current instance as InnerException
                raise = new InvalidOperationException(message, raise);
            }
        }
        throw raise;
    }
}
[HttpPost]
公共异步任务更新注册(ApplicationUser UpdateUser)
{
var SavedUser=await UserManager.FindByIdAsync(updateuser.Id);
尝试
{
updateuser.SecurityStamp=SavedUser.SecurityStamp;
updateuser.PasswordHash=SavedUser.PasswordHash;
updateuser.UserName=SavedUser.UserName;
updateuser.Id=SavedUser.Id;
updateuser.PersonalInfo.ID=SavedUser.PersonalInfo.ID;
updateuser.BillingInfo.ID=SavedUser.BillingInfo.ID;
updateuser.DeliveryInfo.ID=SavedUser.DeliveryInfo.ID;
ApplicationDbContext db=新的ApplicationDbContext();
db.Entry(updateUser).State=EntityState.Modified;
db.Entry(updateUser.PersonalInfo).State=EntityState.Modified;
db.Entry(updateUser.BillingInfo).State=EntityState.Modified;
db.Entry(updateUser.DeliveryInfo).State=EntityState.Modified;
等待db.saveChangesSync();
//var result=await UserManager.UpdateAsync(SavedUser);
返回重定向到操作(“索引”、“主页”);
}
捕获(System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
异常raise=dbEx;
foreach(dbEx.EntityValidationErrors中的var validationErrors)
{
foreach(validationErrors.validationErrors中的var validationError)
{
string message=string.Format(“{0}:{1}”,
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
//引发新的异常嵌套
//将当前实例设置为InnerException
raise=新的无效操作异常(消息,raise);
}
}
抛升;
}
}

你能发布
UserManager
声明吗?这个
UserManager
使用什么
DbContext
呢?它被传递到我的AccountController的构造函数中代码'public AccountController(ApplicationUserManager用户管理器,ApplicationSignInManager signInManager){userManager=userManager;signInManager=signInManager;}它是在哪里创建的?它来自由VS'code'var manager=new ApplicationUserManager(new UserStore(context.Get())创建的app_start/IdentityConfig.cs类;我的AccountController中的所有内容都使用相同的UserManager,但当调用Update方法时,它不起作用