asp.netCore EFcore连接多个数据库

1.首先要有对应的context实体类,

多个实体类的构造函数的参数都应该是集合

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

public class firstContext : DbContext

    {

        //多个数据库应该使用这个构造函数,参数是上下文的集合

        public GalpOnlineContext(DbContextOptions<firstContext> options) : base(options)

        {

        }

        //自定义DbContext实体属性名与数据库表对应名称(默认 表名与属性名对应是 User与Users)

        protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            modelBuilder.Entity<User>().ToTable("user");

            modelBuilder.Entity<Project>().ToTable("project");

            //相关表名称的和类的对应

            base.OnModelCreating(modelBuilder);

        }

        public DbSet<User> User { getset; }

      //....

        //第二种方法,重载父级的构造函数,和配置,这个只能是一个数据库时候的构造函数

        /*

        public firstContext(DbContextOptions options) : base(options)

        {

            // Using the default constructor

        }*/

    }

}   

 2.在appsettings.json中进行配置数据库连接的信息

1

2

3

4

5

6

7

8

9

10

{

  "Logging": {

    "LogLevel": {

      "Default""Warning"

    }

  },

  "ConnectionStrings": {

    "firstContext""Server=localhost;database=test;uid=root;pwd=123456;sslmode=none",

    "secondContext""Server=localhost;database=test2;uid=root;pwd=123456;sslmode=none"

  },

 3.在startup.cs文件中注册数据库上下文的信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

public void ConfigureServices(IServiceCollection services)

      {

          services.Configure<CookiePolicyOptions>(options =>

          {

              //  options.CheckConsentNeeded = context => false;实现session,默认是true

              options.CheckConsentNeeded = context => false

              options.MinimumSameSitePolicy = SameSiteMode.None;

          });

          //注册数据库的服务

          string connectionString = Configuration.GetConnectionString("firstContext");

          string connectionString2 = Configuration.GetConnectionString("secondContext");

          services.AddDbContext<firstContext>(options => options.UseMySql(connectionString));

          services.AddDbContext<secondContext>(options => options.UseMySql(connectionString2));

          //注册session

          services.AddDistributedMemoryCache();

          services.AddSession(Options =>

        {

            Options.IdleTimeout = TimeSpan.FromSeconds(1000);

            Options.Cookie.HttpOnly = true;

        });

          //services.AddMemoryCache();//使用本地缓存必须添加

          //注册mvc服务

          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

      }

  然后就可以了,在使用的地方引入上下文就可以了

复制代码

 public class TestController : Controller
    {
        public readonly firstContext _context;
        //构造函数,依赖注入数据库上下文就可以了
        public TestController(firstContext context)
        {
            _context = context;
        }
        public ActionResult Index(string page)
        {
            List<Project> projects = _context.Project.ToList();
            ViewBag.projects = projects;
            return View();
        }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值