MVC5 中的 MVC4 到 MVC5 ICollection?
MVC4 to MVC5 ICollection in MVC5?
之前我编写了一个 MVC4 应用程序,这些是我的 classes:Ticket、Category、Department、Depot 和 User(现在是 IdentityModels.cs 中的身份用户)
class之间的关系如下:
一对多用户开票(一个用户可以开很多票)
一对多类别到工单(一张工单可以有一个类别)
部门对用户(一个用户只能有一个部门)
用户一对一Depot(一个用户只能拥有一个Depot)
我的问题是因为我已经从 MVC4 升级到 MVC5,MVC5 用户必须从 IdentityUser 派生,所以我必须删除我的 User.cs 并将代码放入我的 ApplicationUser : IdentityUser
class (如下图)
这样做之后,我的 class 与 public virtual ICollection<User> Users { get; set; }
或 public virtual User User { get; set; }
将出现错误,因为它们没有引用任何东西(因为我已经删除了我的用户 class).
The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?)
我需要将该代码更改为什么才能引用我的 ApplicationUser : IdentityUser
class?
IdentityModels.cs(包括从身份用户派生的新用户 class)
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
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 class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string Description { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Depot> Depots { get; set; }
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new 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
}
Department.cs
public class Department
{
public int DepartmentID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string DepartmentName { get; set; }
public virtual ICollection<User> Users { get; set; } <--Error
}
Ticket.cs
public class Ticket
{
public int? TicketID { get; set; }
[Required(ErrorMessage = "Please enter the description")]
public string Issue { get; set; }
[Display(Name = "Administrator")]
[Required(ErrorMessage = "Please select the Administrator")]
public int IssuedTo { get; set; }
public int Author { get; set; }
[DisplayFormat(NullDisplayText = "No Priority")]
public Priority Priority { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; } <--Error
}
旧 user.cs 文件未包含在解决方案中
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; }
}
Configuration.cs(种子方法)开始
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
您基本上是在重新创建实体之间的 relationship/navigation。
Depot.cs
public class Depot {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Department.cs
public class Department {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Ticket.cs
public class Ticket {
//....code removed for brevity
public string UserID { get; set; } // fixed
[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; } // fixed
}
之前我编写了一个 MVC4 应用程序,这些是我的 classes:Ticket、Category、Department、Depot 和 User(现在是 IdentityModels.cs 中的身份用户)
class之间的关系如下:
一对多用户开票(一个用户可以开很多票)
一对多类别到工单(一张工单可以有一个类别)
部门对用户(一个用户只能有一个部门)
用户一对一Depot(一个用户只能拥有一个Depot)
我的问题是因为我已经从 MVC4 升级到 MVC5,MVC5 用户必须从 IdentityUser 派生,所以我必须删除我的 User.cs 并将代码放入我的 ApplicationUser : IdentityUser
class (如下图)
这样做之后,我的 class 与 public virtual ICollection<User> Users { get; set; }
或 public virtual User User { get; set; }
将出现错误,因为它们没有引用任何东西(因为我已经删除了我的用户 class).
The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?)
我需要将该代码更改为什么才能引用我的 ApplicationUser : IdentityUser
class?
IdentityModels.cs(包括从身份用户派生的新用户 class)
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
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 class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string Description { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Depot> Depots { get; set; }
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new 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
}
Department.cs
public class Department
{
public int DepartmentID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string DepartmentName { get; set; }
public virtual ICollection<User> Users { get; set; } <--Error
}
Ticket.cs
public class Ticket
{
public int? TicketID { get; set; }
[Required(ErrorMessage = "Please enter the description")]
public string Issue { get; set; }
[Display(Name = "Administrator")]
[Required(ErrorMessage = "Please select the Administrator")]
public int IssuedTo { get; set; }
public int Author { get; set; }
[DisplayFormat(NullDisplayText = "No Priority")]
public Priority Priority { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; } <--Error
}
旧 user.cs 文件未包含在解决方案中
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; }
}
Configuration.cs(种子方法)开始
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
您基本上是在重新创建实体之间的 relationship/navigation。
Depot.cs
public class Depot {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Department.cs
public class Department {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Ticket.cs
public class Ticket {
//....code removed for brevity
public string UserID { get; set; } // fixed
[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; } // fixed
}