update

2881099
2023-03-25 13:58:54 +08:00
parent fced1fce5a
commit 39976a00d5
4 changed files with 53 additions and 88 deletions

@@ -2,9 +2,7 @@
```csharp
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql,
"Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10"
)
.UseConnectionString(FreeSql.DataType.MySql, connectionString)
.Build(); //请务必定义成 Singleton 单例模式
```

@@ -41,44 +41,32 @@ Install-Package FreeSql.Provider.Sqlite
## Declaring
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
//Automatically synchronize the entity structure to the database.
//FreeSql will not scan the assembly, and will generate a table if and only when the CRUD instruction is executed.
.UseAutoSyncStructure(true)
//Be sure to define as singleton mode
.Build();
//Note: IFreeSql should be declared as a singleton in the project, not created every time it is used.
```
- .NET Core Singleton
Startup.cs
```csharp
public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IFreeSql>(r =>
{
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
//Automatically synchronize the entity structure to the database.
//FreeSql will not scan the assembly, and will generate a table if and only when the CRUD instruction is executed.
.UseAutoSyncStructure(true)
.Build();
services.AddSingleton<IFreeSql>(fsql);
}
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
//Automatically synchronize the entity structure to the database.
//FreeSql will not scan the assembly, and will generate a table if and only when the CRUD instruction is executed.
.UseAutoSyncStructure(true)
.Build();
return fsql;
});
```
- [.NET Core injects multiple FreeSql instances](https://github.com/dotnetcore/FreeSql/issues/44)
- .NET Framework Singleton
```csharp
public class DB
{
static Lazy<IFreeSql>sqliteLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
//Automatically synchronize the entity structure to the database.
//FreeSql will not scan the assembly, and will generate a table if and only when the CRUD instruction is executed.
.UseAutoSyncStructure(true)
.Build());
static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => {
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true)
.Build();
return fsql;
});
public static IFreeSql Sqlite => sqliteLazy.Value;
}
```

@@ -38,59 +38,38 @@ or
Install-Package FreeSql
Install-Package FreeSql.Provider.Sqlite
```
## 声明
```csharp
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库FreeSql不会扫描程序集只有CRUD时才会生成表。
.Build(); //请务必定义成 Singleton 单例模式
//注意: IFreeSql 在项目中应以单例声明,而不是在每次使用的时候创建。
```
- .NET Core 单例
Startup.cs
```csharp
public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IFreeSql>(r =>
{
Func<IServiceProvider, IFreeSql> implementationFreeSql = r =>
{
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库FreeSql不会扫描程序集只有CRUD时才会生成表。
.Build();
return fsql;
};
services.AddSingleton<IFreeSql>(implementationFreeSql);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//在项目启动时从容器中获取IFreeSql实例并执行一些操作同步表种子数据,FluentAPI等
using(IServiceScope serviceScope = app.ApplicationServices.CreateScope())
{
var fsql = serviceScope.ServiceProvider.GetRequiredService<IFreeSql>();
fsql.CodeFirst.SyncStructure(typeof(Topic));//Topic 为要同步的实体类
}
}
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库FreeSql不会扫描程序集只有CRUD时才会生成表。
.Build();
return fsql;
});
```
- [.NET Core注入多个FreeSql实例](https://freesql.net/extra/idlebus-freesql.html)
- .NET Framework 单例
```csharp
public class DB
{
static Lazy<IFreeSql>sqliteLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true) //自动同步实体结构到数据库FreeSql不会扫描程序集只有CRUD时才会生成表。
.Build());
static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => {
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db")
.UseAutoSyncStructure(true)
.Build();
return fsql;
});
public static IFreeSql Sqlite => sqliteLazy.Value;
}
```
> 注意:`class DB\<T\>` 泛型类不适合定义 static 单例
然后使用时,直接通过 `IFreeSql fsql= DB.Sqlite;` 使用 fsql 了。
> 注意class DB\<T\> 泛型类不适合定义 static 单例
IFreeSql 是 ORM 最顶级对象,所有操作都是使用它的方法或者属性:

@@ -23,7 +23,8 @@ fsql.Aop.CurdAfter += (s, e) =>
默认情况 c# 枚举会映射为 MySql Enum 类型,如果想映射为 int 在 FreeSqlBuilder Build 之后执行以下 Aop 统一处理:
```csharp
fsql.Aop.ConfigEntityProperty += (s, e) => {
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(int);
};
@@ -69,11 +70,11 @@ fsql.Select<T>().WithMemory(list).ToList();
---
### 8、多平台代码参考,使用自定义SqliteProvider,例如SqliteMicrosoft.Data.Sqlite或者反射Mono.Data.Sqlite.
### 8、多平台代码参考使用自定义 SqliteProvider例如 SqliteMicrosoft.Data.Sqlite 或者反射 Mono.Data.Sqlite.
[arm/树莓派](https://github.com/densen2014/FreeSqlDemos/tree/master/ARM_ConsoleApp)
**有条件的同学直接试试 FreeSql.Provider.SqliteCore 包,使用的就是Microsoft.Data.Sqlite驱动.**
**有条件的同学直接试试 FreeSql.Provider.SqliteCore 包使用的就是 Microsoft.Data.Sqlite 驱动.**
1.添加包
@@ -85,14 +86,13 @@ fsql.Select<T>().WithMemory(list).ToList();
2.代码
```csharp
Microsoft.Data.Sqlite.SqliteConnection _database = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source=document.db");
var _database = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source=document.db");
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true) //必须开启,因为Microsoft.Data.Sqlite内插处理有bug
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
.Build();
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true) //必须开启,因为Microsoft.Data.Sqlite内插处理有bug
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
.Build();
```
[UWP](https://github.com/densen2014/FreeSqlDemos/tree/master/UWP1)
@@ -100,22 +100,22 @@ var fsql = new FreeSql.FreeSqlBuilder()
```csharp
using System.Data.SQLite;
string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
SQLiteConnection _database = new SQLiteConnection($"Data Source={dbpath}");
var dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSample.db");
var _database = new SQLiteConnection($"Data Source={dbpath}");
var fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.Build();
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => _database, typeof(FreeSql.Sqlite.SqliteProvider<>))
.Build();
```
[Xamarin Forms,代码较多](https://github.com/densen2014/FreeSqlDemos/tree/master/xamarinFormApps)
主程序,接口获取rovider,各个平台自己实现.
```csharp
if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => DependencyService.Get<ISQLite>().GetConnectionSqlite("document"), typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseNoneCommandParameter(true)
.Build();
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => DependencyService.Get<ISQLite>().GetConnectionSqlite("document"), typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseNoneCommandParameter(true)
.Build();
}
```
@@ -125,7 +125,7 @@ if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.And
---
### 9、 2.6.100升级到3.0.100 后无法连接 sqlserver 提示证书无效, 提示证书链是由不受信任的颁发机构颁发的.
### 9、2.6.100升级到3.0.100 后无法连接 sqlserver 提示证书无效, 提示证书链是由不受信任的颁发机构颁发的.
请尝试:
@@ -133,4 +133,4 @@ if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.And
2.使用FreeSql.Provider.SqlServerForSystem替换FreeSql.Provider.SqlServer
深入讨论请转到 https://github.com/dotnetcore/FreeSql/issues/992#issuecomment-1005305027
深入讨论请转到 https://github.com/dotnetcore/FreeSql/issues/992#issuecomment-1005305027