带有 EF 的 MVC 这个 EntityValidationErrors 是什么意思?

MVC with EF What does this EntityValidationErrors mean?

尝试使用 update-database 更新我的数据库后,我得到一个 EntityValidationError,其中有一个行号 (33),其中有错误和一堆我无法解释的文本。我没有足够的信息,也没有足够的知识来解决这个问题。这是否与我的 classes/Context 或我的配置不正确有关,因为代码中没有显示任何错误。

我不知道我做错了什么,因为当我只有用户、类别和票证时这有效 类。

Configuration.cs

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

        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();   <----LINE 33




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




            };
            departments.ForEach(s => context.Departments.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 },
        new User { FirstMidName = "Andy", LastName = "Domagas",    
            EnrollmentDate = DateTime.Parse("2016-02-18"),DepartmentID = 1,DepotID =1  },
        new User { FirstMidName = "Denis",   LastName = "Djohar",     
            EnrollmentDate = DateTime.Parse("2016-02-18") ,DepartmentID = 1,DepotID =1 },
        new User { FirstMidName = "Christine",   LastName = "West",     
            EnrollmentDate = DateTime.Parse("2016-02-18") ,DepartmentID = 2,DepotID =2 },

    };

            users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, 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 = ("Test Error 1"),
                    Priority = Priority.High 
                },
                new Ticket { 
                    UserID = users.Single(s => s.LastName == "Wan").UserID, 
                    CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID, 
                    Issue = ("Test Error 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();





            var administrator = new List<Administrator>
            {
                new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s => s.UserID == 1),
                Tickets = new List<Ticket>() },
                new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s => s.UserID == 2),
                Tickets = new List<Ticket>() },
                new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s => s.UserID == 3),
                Tickets = new List<Ticket>() }



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

IssueContext.cs

public class IssueContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Administrator> Administrators { get; set; }
        public DbSet<Depot> Depots { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Ticket>()
                .HasMany(c => c.Administrators).WithMany(i => i.Tickets)
                .Map(t => t.MapLeftKey("TicketID")
                    .MapRightKey("AdministratorID")
                    .ToTable("AdministratorsTickets"));


            modelBuilder.Entity<Administrator>()
                       .HasKey(e => e.UserID);

            modelBuilder.Entity<User>()
                .HasOptional(s => s.Administrator) // Mark StudentAddress is optional for Student
                .WithRequired(ad => ad.User); // Create inverse relationship


        }
    }

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; }
    }

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; }

    }

Depot.cs

public class Depot
{

    public int DepotID { get; set; }

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

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

}

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; }
}

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; }
}

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; }

}

验证失败,因为部门名称的最小字符串长度为 3,而您创建的第一个字符串只有两个字符长:"IT".

Hth