Skip to content

Commit f4c9bd5

Browse files
2881028810
authored andcommitted
update readme
1 parent 647c93f commit f4c9bd5

File tree

2 files changed

+126
-11
lines changed

2 files changed

+126
-11
lines changed

Extensions/FreeSql.Extensions.EfCoreFluentApi/ICodeFirstExtensions.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
这个 FreeSql 扩展包,实现与 EfCore FluentApi 95% 相似的使用习惯;
2+
3+
## 以假乱真
4+
5+
```csharp
6+
static void Test()
7+
{
8+
ICodeFirst cf = null;
9+
cf.Entity<Song>(eb =>
10+
{
11+
eb.ToTable("tb_song");
12+
eb.Ignore(a => a.Field1);
13+
eb.Property(a => a.Title).HashColumnType("varchar(50)").IsRequired();
14+
eb.Property(a => a.Url).HasMaxLength(100);
15+
16+
eb.Property(a => a.RowVersion).IsRowVersion();
17+
eb.Property(a => a.CreateTime).HasDefaultValueSql("getdate()");
18+
19+
eb.HasKey(a => a.Id);
20+
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_xxx11");
21+
22+
//一对多、多对一
23+
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
24+
25+
//多对多
26+
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
27+
});
28+
}
29+
30+
public class SongType
31+
{
32+
public int Id { get; set; }
33+
public string Name { get; set; }
34+
35+
public List<Song> Songs { get; set; }
36+
}
37+
38+
public class Song
39+
{
40+
public int Id { get; set; }
41+
public string Title { get; set; }
42+
public string Url { get; set; }
43+
public DateTime CreateTime { get; set; }
44+
45+
public int TypeId { get; set; }
46+
public SongType Type { get; set; }
47+
public List<Tag> Tags { get; set; }
48+
49+
public int Field1 { get; set; }
50+
public long RowVersion { get; set; }
51+
}
52+
public class Song_tag
53+
{
54+
public int Song_id { get; set; }
55+
public Song Song { get; set; }
56+
57+
public int Tag_id { get; set; }
58+
public Tag Tag { get; set; }
59+
}
60+
61+
public class Tag
62+
{
63+
[Column(IsIdentity = true)]
64+
public int Id { get; set; }
65+
66+
public string Name { get; set; }
67+
68+
public List<Song> Songs { get; set; }
69+
}
70+
```

0 commit comments

Comments
 (0)