mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 23:40:53 +08:00
Updated FluentApi (markdown)
65
FluentApi.md
65
FluentApi.md
@@ -1,8 +1,13 @@
|
|||||||
FreeSql 提供使用 Fluent Api, 在外部配置实体的数据库特性,Fluent Api 的方法命名与特性名保持一致,如下:
|
## 支持 Fluent API
|
||||||
|
|
||||||
|
FreeSql 提供了 Fluent Api 的方式,使用链式调用,可在外部配置实体的数据库特性。`Fluent Api` 的方法命名与特性名保持一致,共三种使用方法,选择**一种即可**:
|
||||||
|
|
||||||
|
> fsql 是一个 IFreeSql 对象、配置尽量只执行一次,避免性能损耗 参考:[《实体特性说明》](实体特性.md)
|
||||||
|
## ConfigEntity
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
fsql.CodeFirst
|
fsql.CodeFirst
|
||||||
.ConfigEntity<TestFluenttb1>(a =>
|
.ConfigEntity<TestFluenttb1>(a =>
|
||||||
{
|
{
|
||||||
a.Name("xxdkdkdk1");
|
a.Name("xxdkdkdk1");
|
||||||
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
|
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
|
||||||
@@ -28,14 +33,10 @@ class TestFluenttb2 {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
> fsql 是一个 IFreeSql 对象
|
|
||||||
|
|
||||||
> 这段配置尽量只执行一次,避免性能损耗
|
|
||||||
|
|
||||||
参考:[《实体特性说明》](%e5%ae%9e%e4%bd%93%e7%89%b9%e6%80%a7)
|
|
||||||
|
|
||||||
> FreeSql.DbContext v1.4.0+ 实现了 EfCore FluentApi 99% 相似的语法
|
> FreeSql.DbContext v1.4.0+ 实现了 EfCore FluentApi 99% 相似的语法
|
||||||
|
|
||||||
|
## Entity
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
fsql.CodeFirst.Entity<Song>(eb => {
|
fsql.CodeFirst.Entity<Song>(eb => {
|
||||||
eb.ToTable("tb_song");
|
eb.ToTable("tb_song");
|
||||||
@@ -103,6 +104,54 @@ public class Song {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## IEntityTypeConfiguration
|
||||||
|
|
||||||
|
以继承接口`IEntityTypeConfiguration`形式配置实体的。
|
||||||
|
|
||||||
|
- .NET Framework4.0 不支持
|
||||||
|
|
||||||
|
### 实体配置类
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public class SongConfiguration : IEntityTypeConfiguration<Song>
|
||||||
|
{
|
||||||
|
public void Configure(EfCoreTableFluent<Song> eb)
|
||||||
|
{
|
||||||
|
eb.ToTable("tb_song1");
|
||||||
|
eb.Ignore(a => a.Field1);
|
||||||
|
eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
|
||||||
|
eb.Property(a => a.Url).HasMaxLength(100);
|
||||||
|
|
||||||
|
eb.Property(a => a.RowVersion).IsRowVersion();
|
||||||
|
eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
|
||||||
|
|
||||||
|
eb.HasKey(a => a.Id);
|
||||||
|
eb.HasIndex(a => a.Title).IsUnique().HasName("idx_tb_song1111");
|
||||||
|
|
||||||
|
//一对多、多对一
|
||||||
|
eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
|
||||||
|
|
||||||
|
//多对多
|
||||||
|
eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### 二种使用方式
|
||||||
|
|
||||||
|
1.单个配置
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
fsql.CodeFirst.ApplyConfiguration(new SongConfiguration());
|
||||||
|
```
|
||||||
|
|
||||||
|
2.批量配置
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
fsql.CodeFirst.ApplyConfigurationsFromAssembly(typeof(SongConfiguration).Assembly);
|
||||||
|
```
|
||||||
|
|
||||||
## 优先级
|
## 优先级
|
||||||
|
|
||||||
数据库特性 > 实体特性 > FluentApi(配置特性) > Aop(配置特性)
|
数据库特性 > 实体特性 > FluentApi(配置特性) > Aop(配置特性)
|
||||||
|
|||||||
Reference in New Issue
Block a user