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 } }
有没有考虑过用proxy 来处理? 借鉴mysql 的proxy处理方式
有实现或样例吗