Loading

FreeSql.Repository (十一)分表

欢迎来到《FreeSql.Repository 仓储模式》系列文档,本系列文档专注介绍 【仓储+工作单元】 的使用方式。完整文档请前往 wiki 中心:https://github.com/dotnetcore/FreeSql/wiki

FreeSql 支持分表、分库方案,使用仓储提供 AsTable 进行访问更加方便。

var crud = fsql.GetSplitRepository<Log>(null, oldname => $"{oldname}_{DateTime.Now.ToString("YYYYMM")}");

上面我们得到一个日志仓储对象按年月分表,使用它 CURD 最终会操作 Log_202012 表。

注意事项:

  • 使用 CodeFirst.SyncStructure 方法手工迁移分表;
  • 不可在分表分库的实体类型中使用《延时加载》;
public static class YourExtensions
{
    public static IBaseRepository<TEntity> GetSplitRepository<TEntity>(this IFreeSql that, Func<string, string> asTable = null) where TEntity : class
    {
        var repo = new DefaultRepository<TEntity, int>(that, filter);
        repo.AsTable(asTable);
        return repo;
    }
}

系列文章导航

posted @ 2020-12-11 12:14  FreeSql  阅读(1248)  评论(0编辑  收藏  举报