diff --git a/DbFirst.md b/DbFirst.md index 7f0d3f9..89eaab7 100644 --- a/DbFirst.md +++ b/DbFirst.md @@ -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 单例模式 ``` diff --git a/Getting-Started.md b/Getting-Started.md index 393f742..1bc2391 100644 --- a/Getting-Started.md +++ b/Getting-Started.md @@ -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(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(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 LazysqliteLazy = new Lazy(() => 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 sqliteLazy = new Lazy(() => { + var fsql = new FreeSql.FreeSqlBuilder() + .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db") + .UseAutoSyncStructure(true) + .Build(); + return fsql; + }); public static IFreeSql Sqlite => sqliteLazy.Value; } ``` diff --git a/入门.md b/入门.md index 11283da..401566e 100644 --- a/入门.md +++ b/入门.md @@ -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(r => { - Func implementationFreeSql = r => - { - IFreeSql fsql = new FreeSql.FreeSqlBuilder() - .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db") - .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。 - .Build(); - return fsql; - }; - services.AddSingleton(implementationFreeSql); -} - -public void Configure(IApplicationBuilder app, IWebHostEnvironment env) -{ - //在项目启动时,从容器中获取IFreeSql实例,并执行一些操作:同步表,种子数据,FluentAPI等 - using(IServiceScope serviceScope = app.ApplicationServices.CreateScope()) - { - var fsql = serviceScope.ServiceProvider.GetRequiredService(); - 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 LazysqliteLazy = new Lazy(() => new FreeSql.FreeSqlBuilder() - .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=db1.db") - .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。 - .Build()); - + static Lazy sqliteLazy = new Lazy(() => { + 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\` 泛型类不适合定义 static 单例 - -然后使用时,直接通过 `IFreeSql fsql= DB.Sqlite;` 使用 fsql 了。 +> 注意:class DB\ 泛型类不适合定义 static 单例 IFreeSql 是 ORM 最顶级对象,所有操作都是使用它的方法或者属性: diff --git a/常见问题.md b/常见问题.md index fcc3f37..a4ad869 100644 --- a/常见问题.md +++ b/常见问题.md @@ -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().WithMemory(list).ToList(); --- -### 8、多平台代码参考,使用自定义SqliteProvider,例如Sqlite用Microsoft.Data.Sqlite或者反射Mono.Data.Sqlite. +### 8、多平台代码参考,使用自定义 SqliteProvider,例如 Sqlite 用 Microsoft.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().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().GetConnectionSqlite("document"), typeof(FreeSql.Sqlite.SqliteProvider<>)) - .UseNoneCommandParameter(true) - .Build(); + fsql = new FreeSql.FreeSqlBuilder() + .UseConnectionFactory(FreeSql.DataType.Sqlite, () => DependencyService.Get().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 \ No newline at end of file +深入讨论请转到 https://github.com/dotnetcore/FreeSql/issues/992#issuecomment-1005305027