@@ -10,7 +10,7 @@ namespace Routine.APi.Data
1010 public class RoutineDbContext : DbContext
1111 {
1212 //调用并获取父类的options
13- public RoutineDbContext ( DbContextOptions < RoutineDbContext > options ) : base ( options )
13+ public RoutineDbContext ( DbContextOptions < RoutineDbContext > options ) : base ( options )
1414 {
1515
1616 }
@@ -21,10 +21,10 @@ public RoutineDbContext(DbContextOptions<RoutineDbContext>options):base(options)
2121 protected override void OnModelCreating ( ModelBuilder modelBuilder )
2222 {
2323 modelBuilder . Entity < Company > ( ) . Property ( x => x . Name ) . IsRequired ( ) . HasMaxLength ( 100 ) ;
24- modelBuilder . Entity < Company > ( ) . Property ( x=> x . Introduction ) . HasMaxLength ( 500 ) ;
25- modelBuilder . Entity < Employee > ( ) . Property ( x=> x . EmployeeNo ) . IsRequired ( ) . HasMaxLength ( 10 ) ;
26- modelBuilder . Entity < Employee > ( ) . Property ( x=> x . FirstName ) . IsRequired ( ) . HasMaxLength ( 50 ) ;
27- modelBuilder . Entity < Employee > ( ) . Property ( x=> x . LastName ) . IsRequired ( ) . HasMaxLength ( 50 ) ;
24+ modelBuilder . Entity < Company > ( ) . Property ( x => x . Introduction ) . HasMaxLength ( 500 ) ;
25+ modelBuilder . Entity < Employee > ( ) . Property ( x => x . EmployeeNo ) . IsRequired ( ) . HasMaxLength ( 10 ) ;
26+ modelBuilder . Entity < Employee > ( ) . Property ( x => x . FirstName ) . IsRequired ( ) . HasMaxLength ( 50 ) ;
27+ modelBuilder . Entity < Employee > ( ) . Property ( x => x . LastName ) . IsRequired ( ) . HasMaxLength ( 50 ) ;
2828 modelBuilder . Entity < Employee > ( )
2929 //指明一对多关系(可省略)
3030 . HasOne ( x => x . Company )
@@ -33,26 +33,89 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3333 . HasForeignKey ( x => x . CompanyId )
3434 //删除Company时如果有Employee,则无法删除
3535 . OnDelete ( DeleteBehavior . Restrict ) ;
36+ //种子数据
3637 modelBuilder . Entity < Company > ( ) . HasData (
37- new Company
38+ new Company
3839 {
39- Id = Guid . Parse ( "bbdee09c-089b-4d30-bece-44df5923716c" ) ,
40- Name = "Microsoft" ,
41- Introduction = "Great Company"
40+ Id = Guid . Parse ( "bbdee09c-089b-4d30-bece-44df5923716c" ) ,
41+ Name = "Microsoft" ,
42+ Introduction = "Great Company"
4243 } ,
4344 new Company
4445 {
45- Id = Guid . Parse ( "6fb600c1-9011-4fd7-9234-881379716440" ) ,
46- Name = "Google" ,
47- Introduction = "Don't be evil"
46+ Id = Guid . Parse ( "6fb600c1-9011-4fd7-9234-881379716440" ) ,
47+ Name = "Google" ,
48+ Introduction = "Don't be evil"
4849 } ,
4950 new Company
5051 {
51- Id = Guid . Parse ( "5efc910b-2f45-43df-afee-620d40542853" ) ,
52- Name = "Alipapa" ,
53- Introduction = "Fubao Company"
52+ Id = Guid . Parse ( "5efc910b-2f45-43df-afee-620d40542853" ) ,
53+ Name = "Alipapa" ,
54+ Introduction = "Fubao Company"
55+ }
56+ ) ;
57+ modelBuilder . Entity < Employee > ( ) . HasData (
58+ new Employee
59+ {
60+ Id = Guid . Parse ( "ca268a19-0f39-4d8b-b8d6-5bace54f8027" ) ,
61+ CompanyId = Guid . Parse ( "bbdee09c-089b-4d30-bece-44df5923716c" ) ,
62+ DateOfBirth = new DateTime ( 1955 , 10 , 28 ) ,
63+ EmployeeNo = "M001" ,
64+ FirstName = "William" ,
65+ LastName = "Gates" ,
66+ Gender = Gender . 男
67+ } ,
68+ new Employee
69+ {
70+ Id = Guid . Parse ( "265348d2-1276-4ada-ae33-4c1b8348edce" ) ,
71+ CompanyId = Guid . Parse ( "bbdee09c-089b-4d30-bece-44df5923716c" ) ,
72+ DateOfBirth = new DateTime ( 1998 , 1 , 14 ) ,
73+ EmployeeNo = "M024" ,
74+ FirstName = "Kent" ,
75+ LastName = "Back" ,
76+ Gender = Gender . 男
77+ } ,
78+ new Employee
79+ {
80+ Id = Guid . Parse ( "47b70abc-98b8-4fdc-b9fa-5dd6716f6e6b" ) ,
81+ CompanyId = Guid . Parse ( "6fb600c1-9011-4fd7-9234-881379716440" ) ,
82+ DateOfBirth = new DateTime ( 1986 , 11 , 4 ) ,
83+ EmployeeNo = "G003" ,
84+ FirstName = "Mary" ,
85+ LastName = "King" ,
86+ Gender = Gender . 女
87+ } ,
88+ new Employee
89+ {
90+ Id = Guid . Parse ( "059e2fcb-e5a4-4188-9b46-06184bcb111b" ) ,
91+ CompanyId = Guid . Parse ( "6fb600c1-9011-4fd7-9234-881379716440" ) ,
92+ DateOfBirth = new DateTime ( 1977 , 4 , 6 ) ,
93+ EmployeeNo = "G007" ,
94+ FirstName = "Kevin" ,
95+ LastName = "Richardson" ,
96+ Gender = Gender . 男
97+ } ,
98+ new Employee
99+ {
100+ Id = Guid . Parse ( "a868ff18-3398-4598-b420-4878974a517a" ) ,
101+ CompanyId = Guid . Parse ( "5efc910b-2f45-43df-afee-620d40542853" ) ,
102+ DateOfBirth = new DateTime ( 1964 , 9 , 10 ) ,
103+ EmployeeNo = "A001" ,
104+ FirstName = "Jack" ,
105+ LastName = "Ma" ,
106+ Gender = Gender . 男
107+ } ,
108+ new Employee
109+ {
110+ Id = Guid . Parse ( "2c3bb40c-5907-4eb7-bb2c-7d62edb430c9" ) ,
111+ CompanyId = Guid . Parse ( "5efc910b-2f45-43df-afee-620d40542853" ) ,
112+ DateOfBirth = new DateTime ( 1997 , 2 , 6 ) ,
113+ EmployeeNo = "A201" ,
114+ FirstName = "Lorraine" ,
115+ LastName = "Shaw" ,
116+ Gender = Gender . 女
54117 }
55- ) ;
118+ ) ;
56119 }
57120 }
58121}
0 commit comments