Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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中的MVC4到MVC5 I集合?_C#_Asp.net Mvc_Entity Framework_Asp.net Identity - Fatal编程技术网

C# MVC5中的MVC4到MVC5 I集合?

C# MVC5中的MVC4到MVC5 I集合?,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,以前我编写了一个MVC4应用程序,这些是我的类:票证、类别、部门、仓库和用户(现在是IdentityModels.cs中的身份用户) 这些类之间的关系如下所示: 一对多用户对票据(一个用户可以打开很多票据) 一对多类别到票证(一张票证可以有一个类别) 一对一部门对用户(一个用户只能有一个部门) 一对一站点对用户(一个用户只能有一个站点) 我的问题是因为我已经从MVC4升级到MVC5,MVC5用户必须从IdentityUser派生,所以我必须删除我的user.cs并将代码放入我的应用程序user:

以前我编写了一个MVC4应用程序,这些是我的类:票证、类别、部门、仓库和用户(现在是IdentityModels.cs中的身份用户)

这些类之间的关系如下所示:

一对多用户对票据(一个用户可以打开很多票据)

一对多类别到票证(一张票证可以有一个类别)

一对一部门对用户(一个用户只能有一个部门)

一对一站点对用户(一个用户只能有一个站点)

我的问题是因为我已经从MVC4升级到MVC5,MVC5用户必须从IdentityUser派生,所以我必须删除我的user.cs并将代码放入我的
应用程序user:IdentityUser
类(如下所示)

执行此操作后,我与
公共虚拟ICollection用户{get;set;}
公共虚拟用户{get;set;}
的类将出现错误,因为它们没有引用任何内容(因为我已删除我的用户类)

找不到类型或命名空间名称“User”(是否缺少using指令或程序集引用?)

我需要将代码更改为什么,以便它引用我的
ApplicationUser:IdentityUser

IdentityModels.cs(包括从Identity user派生的新用户类)

公共类应用程序用户:IdentityUser
{
公共异步任务
GenerateUserIdentityAsync(用户管理器)
{
var userIdentity=等待管理器
.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
返回用户身份;
}
公共布尔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;}
}
公共类应用程序角色:IdentityRole
{
公共应用程序角色():基(){}
公共应用程序角色(字符串名称):基(名称){}
公共字符串说明{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共数据库集票证{get;set;}
公共数据库集类别{get;set;}
公共数据库集部门{get;set;}
公共数据库集存储库{get;set;}
静态ApplicationDbContext()
{
SetInitializer(新的ApplicationBinInitializer());
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}
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; } <--Error
}
public class Depot {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
公共级仓库
{
public int DepotID{get;set;}
[StringLength(50,最小长度=1)]
公共字符串DepotName{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; } <--Error
}
public class Depot {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}

我不太明白你的意思。所以ApplicationUser:Identity user和MVC4中的用户是一样的?除了一个派生自Identity user,另一个派生自Identity user之外?如果你查看我的配置.cs(种子方法),为什么它是
public string UserID
而不是
public int UserID
。它过去使用IssueContext(数据类上下文),但现在它需要使用ApplicationContext(Identity.cs)既然ApplicationContext应该从Identity Framework派生,它会是什么呢?
内部密封类配置:dbmigrations配置
?IdentityUser.Id的默认类型是StringThe bait and switch不受欢迎。你问的问题与你最初想知道的完全不同。我不明白d您关于dbcontext的问题
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; }


}
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
    {
public class Depot {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
public class Department {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
public class Ticket {
    //....code removed for brevity
    public string UserID { get; set; } // fixed
    [ForeignKey("UserID")]
    public virtual ApplicationUser User { get; set; } // fixed
}