ORM-FreeSql

[删除(380066935@qq.com或微信通知)]

Explore GitHub

FreeSql is a powerful O/RM component, supports .NET Core 2.1+, .NET Framework 4.0+, and Xamarin.

🛠 Support CodeFirst data migration.

💻 Support DbFirst import entity class from database, or use Generation Tool.

⛳ Support advanced type mapping, such as PostgreSQL array type, etc.

🌲 Support expression functions, and customizable analysis.

🏁 Support one-to-many and many-to-many navigation properties, include and lazy loading.

📃 Support Read/Write separation, Splitting Table/Database, Global filters, Optimistic and pessimistic locker.

🌳 Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/达梦/人大金仓/神舟通用/南大通用/翰高/ClickHouse/MsAccess, etc.


Use FreeSql, keep the original usage.

Use FreeSql.Repository, Repository + UnitOfWork.

Use FreeSql.DbContext, Like efcore.

Use FreeSql.BaseEntity, Simple mode.


 Quick start

dotnet add package FreeSql.Provider.Sqlite


static IFreeSql fsql = new FreeSql.FreeSqlBuilder()

  .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=document.db")

  .UseAutoSyncStructure(true) //automatically synchronize the entity structure to the database

  .Build(); //be sure to define as singleton mode


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 ICollection<Tag> Tags { get; set; }

}

class Song_tag {

  public int Song_id { get; set; }

  public Song Song { get; set; }

  

  public int Tag_id { get; set; }

  public 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 Tag Parent { get; set; }

  

  public ICollection<Song> Songs { get; set; }

  public ICollection<Tag> Tags { get; set; }

}

🔎 Query

//OneToOne、ManyToOne

fsql.Select<Tag>().Where(a => a.Parent.Parent.Name == "English").ToList();


//OneToMany

fsql.Select<Tag>().IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "foo")).ToList();


//ManyToMany

fsql.Select<Song>()

  .IncludeMany(a => a.Tags, then => then.Where(sub => sub.Name == "foo"))

  .Where(s => s.Tags.AsSelect().Any(t => t.Name == "Chinese"))

  .ToList();


//Other

fsql.Select<YourType>()

  .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()

More..


fsql.Select<Song>().Where(a => new[] { 1, 2, 3 }.Contains(a.Id)).ToList();


fsql.Select<Song>().Where(a => a.CreateTime.Date == DateTime.Today).ToList();


fsql.Select<Song>().OrderBy(a => Guid.NewGuid()).Limit(10).ToList();

More..


🚁 Repository

dotnet add package FreeSql.Repository


[Transactional]

public void Add() {

  var repo = ioc.GetService<BaseRepository<Tag>>();

  repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;


  var item = new Tag {

    Name = "testaddsublist",

    Tags = new[] {

      new Tag { Name = "sub1" },

      new Tag { Name = "sub2" }

    }

  };

  repo.Insert(item);

}

Reference: Use TransactionalAttribute and UnitOfWorkManager in ASP.NET Core to Achieve the Multiple Transaction Propagation.


💪 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