EntityFramework Core 怎么实现读写分离
展开全部
using Microsoft.EntityFrameworkCore; namespace WebEF.Models { public class SContext : DbContext { public SContext(WriteAndRead writeRead) : base(SjContext.GetOptions(writeRead)) { // TODO: #639 //ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; } public DbSet<User> User { get; set; } } public class SjContext { public static DbContextOptions<SContext> GetOptions(WriteAndRead writeRead) { var optionsBuilder = new DbContextOptionsBuilder<SContext>(); if (writeRead == WriteAndRead.Read) { optionsBuilder.UseSqlServer(@"server=.;uid=sar;pwd=111;database=xxx;"); } else { optionsBuilder.UseSqlServer(@"server=.;uid=sa;pwd=111;database=xxxx;"); } return optionsBuilder.Options; } } public enum WriteAndRead { Write, Read } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询