C# MVC实体框架映射正确,但表不正确

C# MVC实体框架映射正确,但表不正确,c#,entity-framework,asp.net-mvc-4,relational-database,C#,Entity Framework,Asp.net Mvc 4,Relational Database,我目前正在使用MVC4实体框架制作一个票务系统 三类关系 用户到管理员=1:0…1到0或1=少数用户将具有管理员状态,但不是所有用户 管理员到票证=1:*| 1到多个=管理员可以分配多个票证,但一个票证只能分配给一个管理员 用户到票证=1:*1到多个=用户可以打开多个票证,但一个票证只能有一个用户 是我当前表格的样子 我的问题是为什么我的管理表显示不正确 我希望每个票证都由TicketID标识,因此在Admin表中,它应该有AdminID(将解决问题的Admin)、UserID(提交票证的人)和

我目前正在使用MVC4实体框架制作一个票务系统

三类关系 用户到管理员=1:0…1到0或1=少数用户将具有管理员状态,但不是所有用户

管理员到票证=1:*| 1到多个=管理员可以分配多个票证,但一个票证只能分配给一个管理员

用户到票证=1:*1到多个=用户可以打开多个票证,但一个票证只能有一个用户

是我当前表格的样子

我的问题是为什么我的管理表显示不正确

我希望每个票证都由TicketID标识,因此在Admin表中,它应该有AdminID(将解决问题的Admin)、UserID(提交票证的人)和TicketID

我遵循了一个教程,该教程显示了一对多关系,其中团队=管理员(1)对玩家=门票(多)

Player.cs(门票)

Team.cs(管理员)

Administor.cs

public class Administrator
{
    [ForeignKey("User")]
    public int UserID { get; set; }
    [Key]
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]

    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}
公共类管理员
{
[外键(“用户”)]
public int UserID{get;set;}
[关键]
public int AdminID{get;set;}
public int TicketID{get;set;}
[长度(50)]
公共虚拟ICollection票证{get;set;}
公共虚拟用户用户{get;set;}
}
配置.cs

namespace RecreationServicesTicketingSystem.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using RecreationalServicesTicketingSystem.Models;
    using System.Collections.Generic;

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

        protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
        {
            var departments = new List<Department>
            {
                new Department { DepartmentID = 1, Name = "IT"},
                new Department { DepartmentID = 2, Name = "Admin" },
                new Department { DepartmentID = 3, Name = "Human Resources"},
                new Department { DepartmentID = 4, Name = "Mechanics" },
                new Department { DepartmentID = 5, Name = "Directors" },
                new Department { DepartmentID = 6, Name = "Operations"}

            };
            departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();


            var depots = new List<Depot>
            {
                new Depot { DepotID = 1, Name = "Porana"},
                new Depot { DepotID = 2, Name = "Far North"},


            };
            depots.ForEach(s => context.Depots.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();

            var users = new List<User>
        {
            new User { FirstMidName = "Jason",   LastName = "Wan",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1,AdminRole="Administrator LVL1"},
            new User { FirstMidName = "Andy", LastName = "Domagas",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,AdminRole="Administrator LVL2"},
            new User { FirstMidName = "Denis",   LastName = "Djohar",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1,AdminRole="Administrator LVL3"},
            new User { FirstMidName = "Christine",   LastName = "West",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1},

        };


            users.ForEach(s => context.Users.AddOrUpdate(p => p.FirstMidName, s));
            context.SaveChanges();

            users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, s));
            context.SaveChanges();


            var administrator = new List<Administrator>
            {
                new Administrator {AdminID = 1,  User = users.Single ( s => s.UserID == 1),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 2, User = users.Single ( s => s.UserID == 2),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 3,  User = users.Single ( s => s.UserID == 3),
                /*Tickets = new List<Ticket>() */}

            };
            administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
            context.SaveChanges();

            var categories = new List<Category>
            {
                new Category {CategoryID = 0001, Title = "Desktop"},
                new Category {CategoryID = 0002, Title = "Mobile"},
                new Category {CategoryID = 0003, Title = "Menzits"},
                new Category {CategoryID = 0004, Title = "XMPRO"},
                new Category {CategoryID = 0005, Title = "Con-X"},
                new Category {CategoryID = 0006, Title = "Promapp"},
                new Category {CategoryID = 0007, Title = "QGIS"},
            };
            categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title, s));
            context.SaveChanges();

            var tickets = new List<Ticket>
            {
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Con-X" ).CategoryID,
                    Issue = ("Con-X Login Error"),
                    AdminID = 1,
                    Priority = Priority.High
                },
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID,
                    Issue = ("Can't remote access C0123"),
                    AdminID = 2,
                    Priority = Priority.Med
                },
            };


            foreach (Ticket e in tickets)
            {
                var ticketInDataBase = context.Tickets.Where(
                    s =>
                        s.User.UserID == e.UserID &&
                        s.Category.CategoryID == e.CategoryID).SingleOrDefault();
                if (ticketInDataBase == null)
                {
                    context.Tickets.Add(e);
                }
            }
            context.SaveChanges();
        }
    }
}
名称空间RecreationServicesTicketingSystem.Migrations
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.Linq;
使用娱乐服务粘贴系统。模型;
使用System.Collections.Generic;
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
}
受保护的覆盖无效种子(RecreationalServicesTicketingSystem.DAL.IssueContext上下文)
{
var部门=新列表
{
新部门{DepartmentID=1,Name=“IT”},
新部门{DepartmentID=2,Name=“Admin”},
新部门{DepartmentID=3,Name=“人力资源”},
新部门{DepartmentID=4,Name=“Mechanics”},
新部门{DepartmentID=5,Name=“Directors”},
新部门{DepartmentID=6,Name=“Operations”}
};
departments.ForEach(s=>context.departments.AddOrUpdate(p=>p.Name,s));
SaveChanges();
var仓库=新列表
{
新仓库{DepotID=1,Name=“Porana”},
新仓库{DepotID=2,Name=“Far North”},
};
depots.ForEach(s=>context.depots.AddOrUpdate(p=>p.Name,s));
SaveChanges();
var users=新列表
{
新用户{FirstMidName=“Jason”,LastName=“Wan”,
EnrollmentDate=DateTime.Parse(“2016-02-18”),部门ID=1,DepotID=1,AdminRole=“Administrator LVL1”},
新用户{FirstMidName=“Andy”,LastName=“Domagas”,
EnrollmentDate=DateTime.Parse(“2016-02-18”),部门ID=1,DepotID=1,AdminRole=“Administrator LVL2”},
新用户{FirstMidName=“Denis”,LastName=“Djohar”,
EnrollmentDate=DateTime.Parse(“2016-02-18”),部门ID=1,DepotID=1,AdminRole=“Administrator LVL3”},
新用户{FirstMidName=“Christine”,LastName=“West”,
EnrollmentDate=DateTime.Parse(“2016-02-18”),部门ID=1,DepotID=1},
};
users.ForEach(s=>context.users.AddOrUpdate(p=>p.FirstMidName,s));
SaveChanges();
users.ForEach(s=>context.users.AddOrUpdate(p=>p.LastName,s));
SaveChanges();
var管理员=新列表
{
新管理员{AdminID=1,User=users.Single(s=>s.UserID==1),
/*票证=新列表()*/},
新管理员{AdminID=2,User=users.Single(s=>s.UserID==2),
/*票证=新列表()*/},
新管理员{AdminID=3,User=users.Single(s=>s.UserID==3),
/*票证=新列表()*/}
};
administrator.ForEach(s=>context.Administrators.AddOrUpdate(p=>p.AdminID,s));
SaveChanges();
变量类别=新列表
{
新类别{CategoryID=0001,Title=“Desktop”},
新类别{CategoryID=0002,Title=“Mobile”},
新类别{CategoryID=0003,Title=“Menzits”},
新类别{CategoryID=0004,Title=“XMPRO”},
新类别{CategoryID=0005,Title=“Con-X”},
新类别{CategoryID=0006,Title=“Promapp”},
新类别{CategoryID=0007,Title=“QGIS”},
};
categories.ForEach(s=>context.categories.AddOrUpdate(p=>p.Title,s));
SaveChanges();
var tickets=新列表
{
新票{
UserID=users.Single(s=>s.LastName==“Wan”).UserID,
类别ID=类别。单个(c=>c.标题==“Con-X”)。类别ID,
问题=(“Con-X登录错误”),
AdminID=1,
优先级=优先级。高
},
新票{
UserID=users.Single(s=>s.LastName==“Wan”).UserID,
CategoryID=类别。单个(c=>c.Title==“桌面”)。CategoryID,
问题=(“无法远程访问C0123”),
AdminID=2,
优先级=优先级.Med
},
};
foreach(票证中的票证e)
{
var ticketInDataBase=context.Tickets.Where(
    public class User
    {
        [Key]
        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 AdminiID { get; set; }
        [ForeignKey("AdminID")]
        public virtual Administrator Administrator { get; set; }

        public string AdminRole { 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; }
        public virtual ICollection<Ticket> Tickets { get; set; }

    }
public class Ticket
{
    public int TicketID { get; set; }
    public string Issue { 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 AdminID { get; set; }
    //[ForeignKey("AdminID")]
    //public Administrator Admin { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    public int UserID { get; set; }
}
public class Administrator
{
    [ForeignKey("User")]
    public int UserID { get; set; }
    [Key]
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]

    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}
namespace RecreationServicesTicketingSystem.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using RecreationalServicesTicketingSystem.Models;
    using System.Collections.Generic;

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

        protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
        {
            var departments = new List<Department>
            {
                new Department { DepartmentID = 1, Name = "IT"},
                new Department { DepartmentID = 2, Name = "Admin" },
                new Department { DepartmentID = 3, Name = "Human Resources"},
                new Department { DepartmentID = 4, Name = "Mechanics" },
                new Department { DepartmentID = 5, Name = "Directors" },
                new Department { DepartmentID = 6, Name = "Operations"}

            };
            departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();


            var depots = new List<Depot>
            {
                new Depot { DepotID = 1, Name = "Porana"},
                new Depot { DepotID = 2, Name = "Far North"},


            };
            depots.ForEach(s => context.Depots.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();

            var users = new List<User>
        {
            new User { FirstMidName = "Jason",   LastName = "Wan",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1,AdminRole="Administrator LVL1"},
            new User { FirstMidName = "Andy", LastName = "Domagas",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,AdminRole="Administrator LVL2"},
            new User { FirstMidName = "Denis",   LastName = "Djohar",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1,AdminRole="Administrator LVL3"},
            new User { FirstMidName = "Christine",   LastName = "West",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1},

        };


            users.ForEach(s => context.Users.AddOrUpdate(p => p.FirstMidName, s));
            context.SaveChanges();

            users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, s));
            context.SaveChanges();


            var administrator = new List<Administrator>
            {
                new Administrator {AdminID = 1,  User = users.Single ( s => s.UserID == 1),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 2, User = users.Single ( s => s.UserID == 2),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 3,  User = users.Single ( s => s.UserID == 3),
                /*Tickets = new List<Ticket>() */}

            };
            administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
            context.SaveChanges();

            var categories = new List<Category>
            {
                new Category {CategoryID = 0001, Title = "Desktop"},
                new Category {CategoryID = 0002, Title = "Mobile"},
                new Category {CategoryID = 0003, Title = "Menzits"},
                new Category {CategoryID = 0004, Title = "XMPRO"},
                new Category {CategoryID = 0005, Title = "Con-X"},
                new Category {CategoryID = 0006, Title = "Promapp"},
                new Category {CategoryID = 0007, Title = "QGIS"},
            };
            categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title, s));
            context.SaveChanges();

            var tickets = new List<Ticket>
            {
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Con-X" ).CategoryID,
                    Issue = ("Con-X Login Error"),
                    AdminID = 1,
                    Priority = Priority.High
                },
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID,
                    Issue = ("Can't remote access C0123"),
                    AdminID = 2,
                    Priority = Priority.Med
                },
            };


            foreach (Ticket e in tickets)
            {
                var ticketInDataBase = context.Tickets.Where(
                    s =>
                        s.User.UserID == e.UserID &&
                        s.Category.CategoryID == e.CategoryID).SingleOrDefault();
                if (ticketInDataBase == null)
                {
                    context.Tickets.Add(e);
                }
            }
            context.SaveChanges();
        }
    }
}