FreeSql is a powerful.NET ORM library, supporting.NET Framework 4.0+,.NetCore 2.1+, Xamarin and other support for all running platforms of NetStandard. (QQ group: 4336577)
model
FreeSql performs data access using a model, which is represented by an entity class representing a database table or view that is used to query and store data.
Entity model can be generated from an existing database, FreeSql provides IDbFirst interface to achieve entity model generation.
Or manually create the model, create or modify the database based on the model, and provide apis for ICodeFirst synchronization structures (even automatic synchronization at development time).
using FreeSql.DataAnnotations;
using System;
public class Blog
{
[Column(IsIdentity = true, IsPrimary = true)]
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }}Copy the code
The statement
dotnet add packages FreeSql.Provider.Sqlite
var connstr = @"Data Source=|DataDirectory|\db1.db; Attachs=db2.db; Pooling=true; Max Pool Size=10";
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, connstr)
.UseAutoSyncStructure(true) // Automatically synchronize entity structure to database
.Build(); // Be sure to define the Singleton Singleton pattern
Copy the code
Note: IFreeSql should be declared as a singleton in a project, not created every time it is used.
The migration
FreeSql will check the AutoSyncStructure parameter during the program operation, and judge whether to compare the changes between entities and database structures according to this condition, so as to achieve the purpose of automatic migration.
The query
var blogs = fsql.Select<Blog>()
.Where(b => b.Rating > 3)
.OrderBy(b => b.Url)
.Skip(100)
.Limit(10) // Line 100 - line 110
.ToList();
Copy the code
insert
var blog = new Blog { Url = "http://sample.com" };
blog.BlogId = (int)fsql.Insert<Blog>()
.AppendData(blog)
.ExecuteIdentity();
Copy the code
update
fsql.Update<Blog>()
.Set(b => b.Url, "http://sample2222.com")
.Where(b => b.Url == "http://sample.com")
.ExecuteAffrows();
Copy the code
delete
fsql.Delete<Blog>()
.Where(b => b.Url == "http://sample.com")
.ExecuteAffrows();
Copy the code
FreeSqlBuilder
methods | The return value | instructions |
---|---|---|
UseConnectionString | this | Set connection string |
UseSlave | this | Set up the slave database, support multiple |
UseConnectionFactory | this | Set up custom database connection objects (forgo built-in object connection pooling technology) |
UseAutoSyncStructure | this | Automatically synchronize entity structure to database, check entity creation or modify table structure during program running |
UseSyncStructureToLower | this | Change to lowercase synchronization structure, applicable to PostgreSQL |
UseSyncStructureToUpper | this | Switch to uppercase synchronous structure for Oracle/ Damone |
UseNoneCommandParameter | this | Don’t use the command execution parameterization, in view of the Insert/Update, also can use temporary IInsert/IUpdate NoneParameter () |
UseGenerateCommandParameterWithLambda | this | Generate command parameterized execution for lambda expression parsing |
UseLazyLoading | this | The delay loading function is enabled |
UseMonitorCommand | this | Monitor global SQL before and after execution |
UseEntityPropertyNameConvert | this | Entity Property -> Db Filed automatically |
Build<T> | IFreeSql<T> | Create an IFreeSql object, note: singleton design, do not repeat the creation |
ConnectionStrings
DataType | ConnectionString |
---|---|
DataType.MySql | Data Source = 127.0.0.1; Port=3306; User ID=root; Password=root; Initial Catalog=cccddd; Charset=utf8; SslMode=none; Min pool size=1 |
DataType.PostgreSQL | The Host = 192.168.164.10; Port=5432; Username=postgres; Password=123456; Database=tedb; Pooling=true; Minimum Pool Size=1 |
DataType.SqlServer | Data Source=.; Integrated Security=True; Initial Catalog=freesqlTest; Pooling=true; Min Pool Size=1 |
DataType.Oracle | user id=user1; password=123456; Data source = / / 127.0.0.1:1521 / XE; Pooling=true; Min Pool Size=1 |
DataType.Sqlite | Data Source=|DataDirectory|\document.db; Attachs=xxxtb.db; Pooling=true; Min Pool Size=1 |
DataType.OdbcMySql | Driver={MySQL ODBC 8.0 Unicode Driver}; Server = 127.0.0.1; Persist Security Info=False; Trusted_Connection=Yes; UID=root; PWD=root; DATABASE=cccddd_odbc; Charset=utf8; SslMode=none; Min Pool Size=1 |
DataType.OdbcSqlServer | Driver={SQL Server}; Server=.; Persist Security Info=False; Trusted_Connection=Yes; Integrated Security=True; DATABASE=freesqlTest_odbc; Pooling=true; Min Pool Size=1 |
DataType.OdbcOracle | Driver={Oracle in XE}; Server = / / 127.0.0.1:1521 / XE; Persist Security Info=False; Trusted_Connection=Yes; UID=odbc1; PWD=123456; Min Pool Size=1 |
DataType.OdbcPostgreSQL | Driver={PostgreSQL Unicode(x64)}; Server = 192.168.164.10; Port=5432; UID=postgres; PWD=123456; Database=tedb_odbc; Pooling=true; Min Pool Size=1 |
DataType.OdbcDameng | Driver={DM8 ODBC DRIVER}; Server = 127.0.0.1:5236; Persist Security Info=False; Trusted_Connection=Yes; UID=USER1; PWD=123456789 |
DataType.Odbc | Driver={SQL Server}; Server=.; Persist Security Info=False; Trusted_Connection=Yes; Integrated Security=True; DATABASE=freesqlTest_odbc; Pooling=true; Min pool size=1 |
Providers
Package Name | Version |
---|---|
FreeSql.Provider.MySql | NETStandard2.0, net45, net40 |
FreeSql.Provider.MySqlConnector | NETStandard2.0, net45 |
FreeSql.Provider.PostgreSQL | NETStandard2.0, net45 |
FreeSql.Provider.SqlServer | NETStandard2.0, net45, net40 |
FreeSql.Provider.Sqlite | NETStandard2.0, net45, net40 |
FreeSql.Provider.Oracle | NETStandard2.0, net45, net40 |
FreeSql.Provider.Odbc | NETStandard2.0, net45, net40 |
FreeSql.Extensions.LazyLoading | NETStandard2.0, net45, net40 |
FreeSql.Extensions.JsonMap | NETStandard2.0, net45, net40 |
FreeSql.Extensions.BaseEntity | NETStandard2.0 |
Series article navigation
-
(1) Introduction
-
(2) Automatic migration of entities
-
(3) Entity characteristics
-
(4) Solid features of Fluent Api
-
(5) Insert data
-
(6) Batch insert data
-
(7) Ignore columns when inserting data
-
(8) Specify columns when inserting data
-
(9) Delete data
-
(x) Update data
-
(11) Update data Where
-
(12) Specify columns when updating data
-
(13) Ignore columns when updating data
-
(14) Batch update data
-
(15) Query data
-
(16) paging query
-
(17) joint table query
-
(18) Navigation attributes
-
(19) multi-table query
-
(20) query where ecascade
-
(21) Query returned data
-
(22) Dto mapping query
-
(23) Grouping and aggregation
-
(24) Introduction To Linq To Sql syntax
-
(25) delayed loading
-
Include, IncludeMany, Dto, ToList
-
(27) the SQL statement has been written, and entity class mapping for the second query
-
(28) Business
-
Lambda expression
-
(30) Reading and writing separation
-
(31) Zoning table
-
(32) Aop
-
CodeFirst type mapping
-
(34) CodeFirst migration instructions
-
CodeFirst custom features
The resources
Beginner’s Guide | “Select” | “Update” | “Insert” | “Delete” | |
Expression function | “CodeFirst” | “DbFirst” | “The BaseEntity” | |
“Repository” | “The UnitOfWork” | The Filter | Optimism Lock | “The DbContext” | |
Unread | Partition table | “The tenants” | The AOP | Black Tech | Update log |