/FreeSql

FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.

Primary LanguageC#MIT LicenseMIT

FreeSql 是一个功能强大的对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.5+(QQ群:4336577)。

据了解,用户使用很少问问题,编码过程中,因业务阻塞,情有可原;因框架使用问题阻塞,得不偿失。我们的口号:做 .net 最方便的 ORM!愿每一位开发者嘴角上扬😏!

扶摇直上,至强ORM只为自由编码;鹏程万里,至简Linq可使保留黑发;横批:FreeSql(诗人:Coder)

Features

  • 支持 CodeFirst 迁移;
  • 支持 DbFirst 从数据库导入实体类;
  • 大量采用 ExpressionTree 提升性能;
  • 支持深入的类型映射,比如pgsql的数组类型,堪称匠心制作;
  • 支持丰富的表达式函数;
  • 支持导航属性一对多、多对多贪婪加载,以及延时加载;
  • 支持同步/异步数据库操作方法;
  • 支持读写分离、分表分库,租户设计,过滤器,乐观锁;
  • 支持多种数据库,MySql/SqlServer/PostgreSQL/Oracle/Sqlite;
image 《Select》 | 《Update》 | 《Insert》 | 《Delete》
image 《表达式函数》 | 《CodeFirst》 | 《DbFirst》
image 《Repository》 | 《UnitOfWork》 | 《过滤器》 | 《乐观锁》 | 《DbContext》
image 《读写分离》 | 《分区分表》 | 《租户》 | 《AOP》 | 更新日志

Packages

Package Name NuGet Downloads
FreeSql nuget stats
FreeSql.Repository nuget stats
FreeSql.DbContext nuget stats
FreeSql.AdminLTE nuget stats
FreeSql.Connection.Extensions nuget stats
  • FreeSql 是核心,提供原始用法;
  • FreeSql.DbContext 是扩展包,提供面向对象的用法(像EF);
  • FreeSql.Repository 也是扩展包,提供仓储+工作单元用法;
  • FreeSql.Connection.Extensions 也是扩展包,提供像 Dapper 一样的用法;
  • FreeSql.Tools 生成器,基于 razor 模板的生成器;
  • ABP 使用 FreeSql ORM,测试中...;
  • FreeSql 优势.pptx

Providers

Package Name Version
FreeSql.Provider.MySql NETStandard2.0、net452
FreeSql.Provider.PostgreSQL NETStandard2.0、net45
FreeSql.Provider.SqlServer NETStandard2.0、net451
FreeSql.Provider.Sqlite NETStandard2.0、net45
FreeSql.Provider.Oracle NETStandard2.0、net45
FreeSql.Extensions.LazyLoading NETStandard2.0、net45

Quick start

dotnet add package FreeSql.Provider.Sqlite

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.Sqlite, 
        @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build();

class Song {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Url { get; set; }
    public DateTime CreateTime { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}
class Song_tag {
    public int Song_id { get; set; }
    public virtual Song Song { get; set; }

    public int Tag_id { get; set; }
    public virtual Tag Tag { get; set; }
}
class Tag {
    [Column(IsIdentity = true)]
    public int Id { get; set; }
    public string Name { get; set; }

    public int? Parent_id { get; set; }
    public virtual Tag Parent { get; set; }

    public virtual ICollection<Song> Songs { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

Query

//OneToOne、ManyToOne
var t0 = fsql.Select<Tag>()
    .Where(a => a.Parent.Parent.Name == "粤语")
    .IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx"))
    .ToList();

//OneToMany
var t1 = fsql.Select<Tag>()
    .Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10))
    .ToList();

//ManyToMany
var t2 = fsql.Select<Song>()
    .Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语"))
    .IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "xxx"))
    .ToList();

//Other
var t3 = fsql.Select<Xxx>()
    .Where(a => a.IsDelete == 0)
    .WhereIf(keyword != null, a => a.UserName.Contains(keyword))
    .WhereIf(role_id > 0, a => a.RoleId == role_id)
    .Where(a => a.Nodes.AsSelect().Any(t => t.Parent.Id == t.UserId))
    .Count(out var total)
    .Page(page, size)
    .OrderByDescending(a => a.Id)
    .ToList()

更多前往Wiki:《Select 查询数据文档》

var t3 = fsql.Select<Song>()
    .Where(a => new[] { 1, 2, 3 }.Contains(a.Id))
    .ToList();
var t4 = fsql.Select<Song>()
    .Where(a => a.CreateTime.Date == DateTime.Now.Date)
    .ToList();
var t5 = fsql.Select<Song>()
    .OrderBy(a => Guid.NewGuid())
    .Limit(1)
    .ToList();

更多前往Wiki:《表达式函数》

Repository & UnitOfWork

dotnet add package FreeSql.Repository

using (var uow = fsql.CreateUnitOfWork()) {
    var repo1 = uow.GetRepository<Song, int>();
    var repo2 = uow.GetRepository<Tag, int>();

    await repo1.InsertAsync(new Song());
    await repo2.InsertAsync(new Tag());
    uow.Commit();
}

DbContext & DbSet

dotnet add package FreeSql.DbContext

using (var ctx = new fsql.CreateDbContext()) {
    var songs = ctx.Set<Song>();
    var tags = ctx.Set<Tag>();

    var tag = new Tag {
        Name = "testaddsublist",
        Tags = new[] {
            new Tag { Name = "sub1" },
            new Tag { Name = "sub2" },
            new Tag {
                Name = "sub3",
                Tags = new[] {
                    new Tag { Name = "sub3_01" }
                }
            }
        }
    };
    //tags.Add(tag);
    ctx.Add(tag);
    await ctx.SaveChangesAsync();
}

DataFilter & Tenant

public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton<IFreeSql>(Fsql);
    services.AddFreeRepository(filter => filter
        .Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
        .Apply<ITenant>("Tenant", a => a.TenantId == 1)
        ,
        this.GetType().Assembly
    );
}

Temporary disable:

var repoq = fsql.GetRepository<Song, int>();

using (repo1.DataFilter.Disable("Tenant")) {
    //Tenant Invalid
}
//Tenant restore

Performance

FreeSql Query & Dapper Query

Elapsed: 00:00:00.6733199; Query Entity Counts: 131072; ORM: Dapper

Elapsed: 00:00:00.4554230; Query Tuple Counts: 131072; ORM: Dapper

Elapsed: 00:00:00.6846146; Query Dynamic Counts: 131072; ORM: Dapper

Elapsed: 00:00:00.6818111; Query Entity Counts: 131072; ORM: FreeSql*

Elapsed: 00:00:00.6060042; Query Tuple Counts: 131072; ORM: FreeSql*

Elapsed: 00:00:00.4211323; Query ToList<Tuple> Counts: 131072; ORM: FreeSql*

Elapsed: 00:00:01.0236285; Query Dynamic Counts: 131072; ORM: FreeSql*

FreeSql ToList & Dapper Query

Elapsed: 00:00:00.6707125; ToList Entity Counts: 131072; ORM: FreeSql*

Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper

Test code

Contributors

systemhejiyongLambertWmypeng1985stulzqmovingsamALer-Rzouql、 深圳|凉茶