mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 07:20:51 +08:00
1
@@ -31,7 +31,8 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
|||||||
//Be sure to define as singleton mode
|
//Be sure to define as singleton mode
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
public class Song {
|
public class Song
|
||||||
|
{
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
@@ -52,7 +53,8 @@ var curd = fsql.GetRepository<Song>();
|
|||||||
Method 2. Inheritance
|
Method 2. Inheritance
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class SongRepository : BaseRepository<Song, int> {
|
public class SongRepository : BaseRepository<Song, int>
|
||||||
|
{
|
||||||
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
||||||
|
|
||||||
//Do something except CURD.
|
//Do something except CURD.
|
||||||
@@ -62,19 +64,16 @@ public class SongRepository : BaseRepository<Song, int> {
|
|||||||
Method 3: Dependency Injection
|
Method 3: Dependency Injection
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public void ConfigureServices(IServiceCollection services) {
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
|
||||||
services.AddSingleton<IFreeSql>(Fsql);
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
services.AddFreeRepository(filter => filter
|
services.AddFreeRepository(null, this.GetType().Assembly);
|
||||||
.Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
|
|
||||||
.Apply<ITenant>("Tenant", a => a.TenantId == 1)
|
|
||||||
,
|
|
||||||
this.GetType().Assembly
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Use in the controller
|
//Use in the controller
|
||||||
public SongsController(IBaseRepository<Song> repos1) {
|
public SongsController(IBaseRepository<Song> songRepository)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -176,6 +175,7 @@ Please view the documentation of [Cascade Saving](Cascade-Saving).
|
|||||||
| Orm | IFreeSql | ORM currently in use |
|
| Orm | IFreeSql | ORM currently in use |
|
||||||
| DbContextOptions | DbContextOptions | DbContext settings currently in use,modifying the DbContextOptions will not affect other repository. |
|
| DbContextOptions | DbContextOptions | DbContext settings currently in use,modifying the DbContextOptions will not affect other repository. |
|
||||||
| DataFilter | IDataFilter\<TEntity\> | Repository Filter, valid in this object |
|
| DataFilter | IDataFilter\<TEntity\> | Repository Filter, valid in this object |
|
||||||
|
| UpdateDiy | IUpdate\<TEntity\>| Prepare update, same transaction as warehousing |
|
||||||
| Select | ISelect\<TEntity\> | Prepare to query data |
|
| Select | ISelect\<TEntity\> | Prepare to query data |
|
||||||
|
|
||||||
| Method | Return | Parameter | Description |
|
| Method | Return | Parameter | Description |
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
|||||||
.UseAutoSyncStructure(true) //自动迁移实体的结构到数据库
|
.UseAutoSyncStructure(true) //自动迁移实体的结构到数据库
|
||||||
.Build(); //请务必定义成 Singleton 单例模式
|
.Build(); //请务必定义成 Singleton 单例模式
|
||||||
|
|
||||||
public class Song {
|
public class Song
|
||||||
|
{
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
@@ -50,7 +51,8 @@ var curd = fsql.GetRepository<Song>();
|
|||||||
方法2、继承实现;
|
方法2、继承实现;
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class SongRepository : BaseRepository<Song, int> {
|
public class SongRepository : BaseRepository<Song, int>
|
||||||
|
{
|
||||||
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
||||||
|
|
||||||
//在这里增加 CURD 以外的方法
|
//在这里增加 CURD 以外的方法
|
||||||
@@ -60,19 +62,16 @@ public class SongRepository : BaseRepository<Song, int> {
|
|||||||
方法3、依赖注入;
|
方法3、依赖注入;
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public void ConfigureServices(IServiceCollection services) {
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
|
||||||
services.AddSingleton<IFreeSql>(Fsql);
|
services.AddSingleton<IFreeSql>(Fsql);
|
||||||
services.AddFreeRepository(filter => filter
|
services.AddFreeRepository(null, this.GetType().Assembly);
|
||||||
.Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
|
|
||||||
.Apply<ITenant>("Tenant", a => a.TenantId == 1)
|
|
||||||
,
|
|
||||||
this.GetType().Assembly
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//在控制器使用
|
//在控制器使用
|
||||||
public SongsController(IBaseRepository<Song> repos1) {
|
public SongsController(IBaseRepository<Song> songRepository)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user