public class Department { [Key] public int ID { get; set; } = 0; [Required] public string Name { get; set; } = ""; public virtual ICollection<User> Users { get; set; } }
public class User { [Key] public int ID { get; set; } = 0; public int DepartID { get; set; } = 0; [StringLength(30)] public string Username { get; set; } = ""; [ForeignKey("DepartID")] public virtual Department Department { get; set; } }
protected override void Seed(GCSecurity.MVC.DAL.GCDbContext context) { var departs = new List<Department> { new Department{ ID = 1, Name = "办公室" } }; articles.ForEach(s => context.Articles.Add(s)); context.SaveChanges(); context.Users.Add(new User { ID = 1, Username = "admin", DepartID = 1 }); context.SaveChanges(); }
阿晨1998