public async Task MultiPrimary() { using var conn = CreateConnection(); // 如果实体的 Schema 没有配置表名,则使用类名 await conn.ExecuteAsync( $"drop table if exists {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape};"); var storage = CreateStorage(StorageMode.Insert); var context = new DataFlowContext(null, new SpiderOptions(), new Request(), new Response()); var typeName = typeof(CreateTableEntity8); var entity = new CreateTableEntity8(); var items = new List <CreateTableEntity8> { entity }; context.AddData(typeName, items); await storage.HandleAsync(context); var list = (await conn.QueryAsync <CreateTableEntity8>( $"SELECT * FROM {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape}")) .ToList(); Assert.Single(list); entity = list.First(); Assert.Equal("xxx", entity.Str1); Assert.Equal("yyy", entity.Str2); Assert.Equal(655, entity.Required); Assert.Equal(0, entity.Decimal); Assert.Equal(600, entity.Long); Assert.Equal(400, entity.Double); Assert.Equal(200.0F, entity.Float); var primaries = (await conn.QueryAsync <PrimaryInfo>( $"SELECT t.CONSTRAINT_TYPE, c.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t, INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c WHERE t.TABLE_NAME = c.TABLE_NAME AND t.TABLE_SCHEMA = 'test' AND t.CONSTRAINT_TYPE = 'PRIMARY KEY' AND t.TABLE_NAME='createtablemultiprimay';") ).ToList(); _testOutputHelper.WriteLine(JsonConvert.SerializeObject(primaries)); var columnNames = primaries.Select(x => x.COLUMN_NAME).ToList(); Assert.Equal(2, primaries.Count); Assert.Contains("str2", columnNames); Assert.Contains("decimal", columnNames); await conn.ExecuteAsync( $"drop table if exists {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape};"); }
public async Task MultiPrimary() { using (var conn = CreateConnection()) { // 如果实体的 Schema 没有配置表名,则使用类名 await conn.ExecuteAsync( $"drop table if exists {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape};"); using (var builder = GetLocalSpiderHostBuilder()) { var provider = builder.Build(); var services = provider.CreateScopeServiceProvider(); var storage = CreateStorage(StorageType.Insert); var dfc = new DataFlowContext(null, services); var typeName = typeof(CreateTableEntity8).FullName; var entity = new CreateTableEntity8(); dfc.Add(typeName, entity.GetTableMetadata()); var items = new ParseResult <CreateTableEntity8> { entity }; dfc.AddParseData(typeName, items); await storage.HandleAsync(dfc); var list = (await conn.QueryAsync <CreateTableEntity8>( $"SELECT * FROM {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape}")) .ToList(); Assert.Single(list); entity = list.First(); Assert.Equal("xxx", entity.Str1); Assert.Equal("yyy", entity.Str2); Assert.Equal(655, entity.Required); Assert.Equal(0, entity.Decimal); Assert.Equal(600, entity.Long); Assert.Equal(400, entity.Double); Assert.Equal(200.0F, entity.Float); var primaries = (await conn.QueryAsync <PrimaryInfo>( "SELECT t.CONSTRAINT_TYPE, c.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t, INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c WHERE t.TABLE_NAME = c.TABLE_NAME AND t.TABLE_SCHEMA = 'test' AND t.CONSTRAINT_TYPE = 'PRIMARY KEY' AND t.TABLE_NAME='createtablemultiprimay';") ).ToList(); Assert.Equal(2, primaries.Count); Assert.Equal("str2", primaries[0].COLUMN_NAME); Assert.Equal("decimal", primaries[1].COLUMN_NAME); await conn.ExecuteAsync( $"drop table if exists {Escape}test{Escape}.{Escape}createtablemultiprimay{Escape};"); } } }