Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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#_Asp.net Mvc_Entity Framework_Asp.net Identity - Fatal编程技术网

C# MVC5自定义身份用户

C# MVC5自定义身份用户,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我正在尝试将一些属性添加到我的registerviewmodel中,以便当用户在我的站点上注册时,他们需要添加他们的LastName、FirstMidName、DepotID和DepartmentID。当我试图修改RegisterViewModel创建方法时,出现了错误 DepotID和DepartmentID的RegisterViewModel注册方法错误 public async Task<ActionResult> Register(RegisterViewModel mode

我正在尝试将一些属性添加到我的registerviewmodel中,以便当用户在我的站点上注册时,他们需要添加他们的LastName、FirstMidName、DepotID和DepartmentID。当我试图修改RegisterViewModel创建方法时,出现了错误

DepotID和DepartmentID的RegisterViewModel注册方法错误

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

            user.LastName = model.LastName;
            user.FirstMidName = model.FirstMidName;
            user.Depot = model.DepotID;
            //Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Depot
            user.Department = model.DepartmentID;
            //Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Department
            user.EnrollmentDate = model.EnrollmentDate;
科室

public class Department
{

    public int DepartmentID { get; set; }

    [StringLength(50, MinimumLength = 1)]
    public string DepartmentName { get; set; }

    public virtual ICollection<User> Users { get; set; }
}
公共课部
{
public int DepartmentID{get;set;}
[StringLength(50,最小长度=1)]
公共字符串DepartmentName{get;set;}
公共虚拟ICollection用户{get;set;}
}
Depot.cs

public class Depot
{

    public int DepotID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string DepotName { get; set; }
    public virtual ICollection<User> Users { get; set; }

}
公共级仓库
{
public int DepotID{get;set;}
[StringLength(50,最小长度=1)]
公共字符串DepotName{get;set;}
公共虚拟ICollection用户{get;set;}
}
MVC4中的旧用户类

public class User
{
    public int UserID { get; set; }

    public bool IsAdministrator { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    public string FullName
    {
        get { return FirstMidName +" "+ LastName; }
    }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }
    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }


}
公共类用户
{
public int UserID{get;set;}
公共布尔IsAdministrator{get;set;}
[StringLength(50,最小长度=1)]
公共字符串LastName{get;set;}
[StringLength(50,MinimumLength=1,ErrorMessage=“名字不能超过50个字符)。]
[列(“名字”)]
公共字符串FirstMidName{get;set;}
公共字符串全名
{
获取{return FirstMidName+“”+LastName;}
}
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”,ApplyFormatInEditMode=true)]
公共日期时间注册日期{get;set;}
public int DepartmentID{get;set;}
[外键(“部门ID”)]
公共虚拟部门部门{get;set;}
public int DepotID{get;set;}
[外键(“DepotID”)]
公共虚拟仓库{get;set;}
公共虚拟ICollection票证{get;set;}
}

您正试图将整数值分配给
型号

//Cannot implicitly convert type 'int' to RecreationalServicesTickingSystem.Models.Depot
                user.Department = model.DepartmentID;
//Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Department
                user.EnrollmentDate = model.EnrollmentDate;
您的型号
ApplicationUser

 public int DepartmentID { get; set; }

 [ForeignKey("DepartmentID")]
 public virtual Department Department { get; set; }
正确的代码应为:

//To assign to DepartmentID
  user.DepartmentID = model.DepartmentID;
//OR
//to assign to model's departmentID
  user.Department.DepartmentID = model.DepartmentID;
仓库的情况也一样

//Cannot implicitly convert type 'int' to RecreationalServicesTickingSystem.Models.Depot
                user.Department = model.DepartmentID;
//Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Department
                user.EnrollmentDate = model.EnrollmentDate;
 public int DepartmentID { get; set; }

 [ForeignKey("DepartmentID")]
 public virtual Department Department { get; set; }
//To assign to DepartmentID
  user.DepartmentID = model.DepartmentID;
//OR
//to assign to model's departmentID
  user.Department.DepartmentID = model.DepartmentID;