MVC中的关系数据库映射 [英] Relational Database Mapping in MVC

查看:280
本文介绍了MVC中的关系数据库映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为代码量表示歉意,但我只需要确认我的映射和[ForeignKey("XXX")的用法正确与否即可.

I apologize for the amount of code but I just need some confirmation of my mapping and usage of [ForeignKey("XXX") is correct or not.

在Department和Depot之类的类中,我是否需要行public int DepartmentID { get; set; }或在将数据插入数据库时​​它们会自动编号? (部门ID = 1(人力资源),仓库ID = 2(洛杉矶)

In classes like Department and Depot do I need the lines public int DepartmentID { get; set; } or do they automatically number themselves when data is inserted in the database. (DepartmentID = 1 (Human Resources), DepotID = 2 (Los Angeles)

这是我的实体图.

User.cs

public class User
{

    public int UserID { 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; }

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

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    [ForeignKey("AdministratorID")]
    public virtual Administrator Administrator { 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 int TicketID { get; set; }
    //Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
    public virtual ICollection<Ticket> Users { get; set; }

}

Ticket.cs

Ticket.cs

public class Ticket
{
    public string Issue { get; set; } 
    [DisplayFormat(NullDisplayText = "No Priority")]
    public Priority? Priority { get; set; }
    //Category One to Many Ticket
    public int CategoryID { get; set; }
    [ForeignKey("CategoryID")]
    public virtual Category Category { get; set; }
    //User (One to Many) Ticket
    public int UserID { get; set; }
    public int TicketID { get; set; }
    [ForeignKey("TicketID")]
    public virtual User User { get; set; }
    public int AdminID { get; set; }
    public virtual ICollection<Administrator> Administrators { get; set; }

}

Depot.cs

public class Depot
{
    public int DepotID { get; set; }

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

    public virtual ICollection<User> Users { get; set; }

}

Department.cs

Department.cs

public class Department
{
    public int DepartmentID { get; set; }

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

    public virtual ICollection<User> Users { get; set; }
}

Category.cs

Category.cs

public class Category
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CategoryID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Ticket> Tickets { get; set; }
}

Administrator.cs

Administrator.cs

    public class Administrator
{
    [Key, ForeignKey("User")]
    public int UserID { get; set; }
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]
    public string AdminRole { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}

推荐答案

ID属性将成为对应于此类的数据库表的主键列.默认情况下,实体框架将名为ID或classnameID的属性解释为主键.

The ID property will become the primary key column of the database table that corresponds to this class. By default, the Entity Framework interprets a property that's named ID or classnameID as the primary key.

有关软件仓库和用户的关系,请参见下文(注意如何将用户中的软件仓库和用户仓库标记为虚拟以启用延迟加载)

See below for Depot and User relationships (notice how DepotID and Depot in User are marked as virtual to enable lazy loading)

public class Depot
{      
    public int DepotID { get; set; }

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

    public virtual ICollection<User> Users { get; set; }

}

public class User
{

    public int UserID { 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; }

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

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    public virtual Administrator Administrator { get; set; }

    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }

    public virtual int DepotID { get; set; }

    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }

    public int TicketID { get; set; }
    public virtual ICollection<Ticket> Users { get; set; }

}

这篇关于MVC中的关系数据库映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆