From 839624f6b1d939c286aa7ce89745cbf557c8a80f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 6 Oct 2022 11:51:48 +0800 Subject: [PATCH] Update unit test --- .../SqlServerTest/UnitTest/CrossDatabase01.cs | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs b/Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs index 3e4c1f9d4..3abde434c 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/CrossDatabase01.cs @@ -1,14 +1,77 @@ using System; using System.Collections.Generic; using System.Text; - +using SqlSugar; +using System.Linq; namespace OrmTest { public class CrossDatabase01 { - public static void Init() + public static void Init() { + var db = new SqlSugarClient(new List() + { + new ConnectionConfig(){ConfigId="OrderDb",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",IsAutoCloseConnection=true}, + new ConnectionConfig(){ConfigId="OrderItemDb",DbType=DbType.SqlServer,ConnectionString="server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST2",IsAutoCloseConnection=true } + }); + + db.GetConnection("OrderDb").CodeFirst.InitTables(); + db.GetConnection("OrderItemDb").CodeFirst.InitTables(); + + db.GetConnection("OrderDb").DbMaintenance.TruncateTable(); + db.GetConnection("OrderItemDb").DbMaintenance.TruncateTable(); + + db.GetConnection("OrderDb").Insertable(new Order() { Id = 1, CreateTime = DateTime.Now, Name = "a", Price = 10, CustomId = 1 }).ExecuteCommand(); + db.GetConnection("OrderItemDb").Insertable(new OrderItem() { OrderId = 1, CreateTime = DateTime.Now, Price = 10 }).ExecuteCommand(); + + var list =db.GetConnection("OrderItemDb").Queryable() + .CrossQuery(typeof(Order), "OrderDb") + .Includes(z => z.Order) + .ToList(); + + + var list2 = db.GetConnection("OrderDb").Queryable() + .CrossQuery(typeof(OrderItem), "OrderItemDb") + .Includes(z => z.Items) + .ToList(); + + if (list.First().Order == null) + { + throw new Exception("unit error"); + } + if (list2.First().Items.Count == 0) + { + throw new Exception("unit error"); + } + + } + [SqlSugar.SugarTable("Order8")] + public class Order + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + + public string Name { get; set; } + public decimal Price { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime CreateTime { get; set; } + [SugarColumn(IsNullable = true)] + public int CustomId { get; set; } + [Navigate(NavigateType.OneToMany, nameof(OrderItem.OrderId))] + public List Items { get; set; } + } + [SqlSugar.SugarTable("OrderDetail8")] + public class OrderItem + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int ItemId { get; set; } + public int OrderId { get; set; } + public decimal? Price { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? CreateTime { get; set; } + [Navigate(NavigateType.OneToOne, nameof(OrderId))] + public Order Order { get; set; } } } }