@@ -8,30 +8,75 @@ namespace FreeSql.Extensions.EfCoreFluentApi
88 public static class ICodeFirstExtensions
99 {
1010
11+ public static ICodeFirst Entity < T > ( this ICodeFirst codeFirst , Action < EfCoreTableFluent < T > > modelBuilder )
12+ {
13+ codeFirst . ConfigEntity < T > ( tf => modelBuilder ( new EfCoreTableFluent < T > ( tf ) ) ) ;
14+ return codeFirst ;
15+ }
16+
1117 static void Test ( )
1218 {
1319 ICodeFirst cf = null ;
14- cf . Entity < TestInfo > ( eb =>
20+ cf . Entity < Song > ( eb =>
1521 {
16- eb . Property ( b => b . Name ) . HashColumnType ( "varchar(50)" ) ;
17- eb . Property ( b => b . FullName ) . HashColumnType ( "varchar(60)" ) ;
22+ eb . ToTable ( "tb_song" ) ;
23+ eb . Ignore ( a => a . Field1 ) ;
24+ eb . Property ( a => a . Title ) . HashColumnType ( "varchar(50)" ) . IsRequired ( ) ;
25+ eb . Property ( a => a . Url ) . HasMaxLength ( 100 ) ;
26+
27+ eb . Property ( a => a . RowVersion ) . IsRowVersion ( ) ;
28+ eb . Property ( a => a . CreateTime ) . HasDefaultValueSql ( "getdate()" ) ;
1829
19- eb . HasKey ( a => a . Id ) . HasKey ( a => new { a . Id , a . Name } ) ;
20- eb . HasIndex ( a => a . Name ) . IsUnique ( ) . HasName ( "idx_xxx11" ) ;
30+ eb . HasKey ( a => a . Id ) ;
31+ eb . HasIndex ( a => a . Title ) . IsUnique ( ) . HasName ( "idx_xxx11" ) ;
32+
33+ //一对多、多对一
34+ eb . HasOne ( a => a . Type ) . HasForeignKey ( a => a . TypeId ) . WithMany ( a => a . Songs ) ;
35+
36+ //多对多
37+ eb . HasMany ( a => a . Tags ) . WithMany ( a => a . Songs , typeof ( Song_tag ) ) ;
2138 } ) ;
2239 }
23- class TestInfo
40+
41+ public class SongType
2442 {
2543 public int Id { get ; set ; }
2644 public string Name { get ; set ; }
27- public string FullName { get ; set ; }
28- public int DefaultValue { get ; set ; }
45+
46+ public List < Song > Songs { get ; set ; }
2947 }
3048
31- public static ICodeFirst Entity < T > ( this ICodeFirst codeFirst , Action < EfCoreTableFluent < T > > modelBuilder )
49+ public class Song
3250 {
33- codeFirst . ConfigEntity < T > ( tf => modelBuilder ( new EfCoreTableFluent < T > ( tf ) ) ) ;
34- return codeFirst ;
51+ public int Id { get ; set ; }
52+ public string Title { get ; set ; }
53+ public string Url { get ; set ; }
54+ public DateTime CreateTime { get ; set ; }
55+
56+ public int TypeId { get ; set ; }
57+ public SongType Type { get ; set ; }
58+ public List < Tag > Tags { get ; set ; }
59+
60+ public int Field1 { get ; set ; }
61+ public long RowVersion { get ; set ; }
62+ }
63+ public class Song_tag
64+ {
65+ public int Song_id { get ; set ; }
66+ public Song Song { get ; set ; }
67+
68+ public int Tag_id { get ; set ; }
69+ public Tag Tag { get ; set ; }
70+ }
71+
72+ public class Tag
73+ {
74+ [ Column ( IsIdentity = true ) ]
75+ public int Id { get ; set ; }
76+
77+ public string Name { get ; set ; }
78+
79+ public List < Song > Songs { get ; set ; }
3580 }
3681 }
3782}
0 commit comments