首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC4 to MVC5 ICollection in MVC5?

MVC4 to MVC5 ICollection in MVC5?
EN

Stack Overflow用户
提问于 2016-03-27 02:59:35
回答 1查看 83关注 0票数 1

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

这些类之间的关系如下:

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

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

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

一对一仓库对用户(一个用户只能拥有一个仓库)

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

这样做之后,使用public virtual ICollection<User> Users { get; set; }public virtual User User { get; set; }的类将得到错误,因为它们没有引用任何内容(因为我已经删除了我的用户类)。

无法找到类型或命名空间名称“User”(您是缺少了使用指令还是程序集引用?)

要将该代码更改为引用我的ApplicationUser : IdentityUser类需要什么?

IdentityModels.cs (包括从身份用户派生的新用户类)

代码语言:javascript
复制
   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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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文件未包括在解决方案中

代码语言:javascript
复制
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的开始(种子法)

代码语言:javascript
复制
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
    {
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-27 10:00:22

您基本上是在重新创建实体之间的关系/导航。

Depot.cs

代码语言:javascript
复制
public class Depot {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}

Department.cs

代码语言:javascript
复制
public class Department {
    //....code removed for brevity
    public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}

Ticket.cs

代码语言:javascript
复制
public class Ticket {
    //....code removed for brevity
    public string UserID { get; set; } // fixed
    [ForeignKey("UserID")]
    public virtual ApplicationUser User { get; set; } // fixed
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36243368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档