读写分离
一、数据访问层接口
1.接口定义
代码如下(示例):
using EFCoreDemo.CodeFirst.Migrations.Extend;
using System;
namespace EFCoreDemo.CodeFirst.IService
{
public interface IBaseService : IDisposable
{
#region Query
/// <summary>
/// 根据id查询实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T Find<T>(int id, ReadWriteEnum readWriteEnum = ReadWriteEnum.Read) where T : class;
#endregion
#region Add
/// <summary>
/// 新增数据,即时Commit
/// </summary>
/// <param name="t"></param>
/// <returns>返回带主键的实体</returns>
public T Insert<T>(T t) where T : class;
#endregion
#region Delete
/// <summary>
/// 根据主键删除数据,即时Commit
/// </summary>
/// <param name="t"></param>
public void Delete<T>(int Id) where T : class;
public void Delete<T>(T t) where T : class;
#endregion
#region Other
/// <summary>
/// 立即保存全部修改
/// 把增/删的savechange给放到这里,是为了保证事务的
/// </summary>
void Commit();
#endregion
}
}
2.接口实现
代码如下(示例):
using EFCoreDemo.CodeFirst.IService;
using EFCoreDemo.CodeFirst.Migrations.Extend;
using Microsoft.EntityFrameworkCore;
using System;
namespace EFCoreDemo.CodeFirst.Service
{
public class BaseService : IBaseService, IDisposable
{
protected IDbContextFactory _ContextFactory = null;
protected DbContext _Context {
get

本文详细介绍了如何使用EFCore实现数据库的读写分离,包括数据访问层接口设计、读写操作枚举、连接字符串读取、DbContext扩展以及如何在上端配置数据库连接字符串。通过示例代码展示了各个关键步骤的实现。
最低0.47元/天 解锁文章
1602

被折叠的 条评论
为什么被折叠?



