コード例 #1
0
ファイル: WriteReadTest.cs プロジェクト: JolyneStone/Rye
        public WriteReadTest()
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("appsettings.json")
                         .Build();
            var services = new ServiceCollection()
                           .AddSingleton <IConfiguration>(config)
                           .AddRye()
                           .AddDbConnections()
                           .AddSqlServerEFCore()
                           .AddSingleton <DbContextOptionsBuilderOptions <TestDbContext> >(new DbContextOptionsBuilderOptions <TestDbContext>(new DbContextOptionsBuilder <TestDbContext>(), null));

            _serviceProvider = services.BuildServiceProvider();

            var dbProvider = _serviceProvider.GetRequiredService <IDbProvider>();
            var uow        = dbProvider.GetUnitOfWork <TestDbContext>("RyeTestDb"); // 访问主库

            var repoDbTest = uow.GetRepository <DbTest, int>();
            var obj        = new DbTest {
                Name = "123", Date = DateTime.Now.Date
            };

            repoDbTest.Insert(obj);
            uow.SaveChanges();

            //Console.ReadKey();

            //var uow2 = dbProvider.GetUnitOfWork<TestDbContext>("TestDb_Read"); // 访问从库

            //var repoDbTest2 = uow2.GetReadOnlyRepository<DbTest, int>();
            //var data2 = repoDbTest2.GetFirstOrDefault();
            //Console.WriteLine($"id: {data2.Id} name: {data2.Name}");
            //Console.ReadKey();
        }
コード例 #2
0
ファイル: WriteReadTest.cs プロジェクト: JolyneStone/Rye
        private void Write(out int newId)
        {
            var dbProvider = _serviceProvider.GetRequiredService <IDbProvider>();
            var map        = _serviceProvider.GetService <IOptions <DbConnectionMapOptions> >();
            var uow        = dbProvider.GetUnitOfWork <TestDbContext>("RyeTestDb");

            var repoDbTest = uow.GetRepository <DbTest, int>();
            var obj        = new DbTest {
                Name = "123", Date = DateTime.Now.Date
            };

            repoDbTest.Insert(obj);
            uow.SaveChanges();
            uow.DbContext.AsDbContext().Entry(obj);
            newId = obj.Id;
        }