微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在另一个表的col中显示相关对象

如何解决在另一个表的col中显示相关对象

我创建了一个可以管理员工的网站。这些员工可以属于一个组,您应该可以在其中添加新员工,或者如果需要,可以删除一个。 这是我的网上论坛标签图片

Here is the Groups tab

您会看到“雇员”选项卡为空。 这是我的Employees.cs:

    public class Employee
    {
        public int Id { get; set; }

        [required]
        [MaxLength(50,ErrorMessage = "Name cannot exceed 50 characters")]
        public string Name { get; set; }

        [required]
        [RegularExpression(@"^[a-zA-Z0-9_0+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",ErrorMessage = "Invalid Email Format")]
        [display(Name = "Office Email")]
        public string Email { get; set; }

        public int DepartmentId { get; set; }

        public Department Department { get; set; }

        public int GroupId { get; set; }

        public Group.Group Group { get; set; }
    }

这是我的Group.cs:

    public class Group
    {
        [Key]
        public int GroupId { get; set; }

        [required]
        public string Name { get; set; }

        [required]
        public string Task { get; set; }

        [required]
        public List<Employee> Employees { get; set; }
    }

最后是我的AppDbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Employee>()
                .HasData(
                new Employee() { Id = -99,Name = "Mary",Email = "mary@gmail.com",DepartmentId = -99,GroupId = -1 },new Employee() { Id = -98,Name = "Stan",Email = "stan@gmail.com",new Employee() { Id = -97,Name = "Mike",Email = "mike@gmail.com",GroupId = -1 });

            modelBuilder.Entity<Department>()
                .HasData(
                new Department() { DepartmentId = -99,Field = "IT",Name = "Programming Department" },new Department() { DepartmentId = -98,Field = "HR",Name = "Human Resorcues" },new Department() { DepartmentId = -97,Field = "AD",Name = "Advertisement Department" });

            modelBuilder.Entity<Group.Group>()
                .HasData(
                    new Group.Group() { GroupId = -1,Name = "Cleaner",Task = "Clean the building" }
                );

            modelBuilder.Entity<Problem>()
                .HasData(
                    new Problem() { ProblemId = -99,Title = "Spilled drink in basement",Descrpition = "Someone spilled drink all over the place in the basement",IsDone= false },new Problem() { ProblemId = -98,Title = "Power problems",Descrpition = "Power goes off sometimes in the buildin",IsDone = false },new Problem() { ProblemId = -97,Title = "Internet problems",Descrpition = "Internet is very weak and some devices lose signal",IsDone = false });
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。