using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Net.Mime; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using SqlSugar; namespace OrmTest; public static class Unitdafaaaaa { public static void Init() { Main22().GetAwaiter().GetResult(); } public static async Task Main22() { await Task.CompletedTask; var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() { ConnectionString = $@"DataSource={Environment.CurrentDirectory}\test22.db", DbType = DbType.Sqlite, IsAutoCloseConnection = true, }); db.Aop.OnLogExecuting = (s, parameters) => Console.WriteLine(UtilMethods.GetSqlString(DbType.Sqlite, s, parameters)); db.DbMaintenance.CreateDatabase(); //建表 db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); var item = new ItemInfos { Id = 1 }; var property = new ItemInfoProperty { StockId = 100, ItemId = 1 ,Color="a"}; var stock = new MaterialStocks { Id = 100, ItemId = 1, StockId = 100 }; var order = new Orders { Id = 1, StockId = 100 }; await db.Insertable(item).ExecuteCommandAsync(); await db.Insertable(property).ExecuteCommandAsync(); await db.Insertable(stock).ExecuteCommandAsync(); await db.Insertable(order).ExecuteCommandAsync(); var result = await db.Queryable() .LeftJoin((s, ip) => s.StockId == ip.StockId) .LeftJoin((s, ip, i) => ip.ItemId == i.Id) .Select((s, ip, i) => new View() { ItemInfoProperty = ip, }, true) .ToListAsync(); var result2 = await db.Queryable() .LeftJoin((s, ip) => s.StockId == ip.StockId) .Select((s, ip) => new View() { ItemInfoProperty = ip, }, true) .ToListAsync(); } public class MaterialStocks { [SugarColumn(IsPrimaryKey = true, IsIdentity = false, IsNullable = false)] public long Id { get; set; } /// /// 仓库 /// [SugarColumn(DefaultValue = "0")] public long WarehouseId { get; set; } /// /// 库存Id /// [SugarColumn(DefaultValue = "0")] public long StockId { get; set; } /// /// 物料属性 /// [Navigate(NavigateType.OneToOne, nameof(StockId))] public ItemInfoProperty ItemProperty { get; set; } [SugarColumn(DefaultValue = "0")] public long ProductId { get; set; } [SugarColumn(DefaultValue = "0")] public long ItemId { get; set; } [Navigate(NavigateType.OneToOne, nameof(ItemId))] public ItemInfos ItemInfo { get; set; } /// /// 账面库存 /// [SugarColumn(Length = 18, DecimalDigits = 4, DefaultValue = "0")] public decimal StockQty { get; set; } /// /// 可用库存 /// [SugarColumn(Length = 18, DecimalDigits = 4, DefaultValue = "0")] public decimal AliveQty { get; set; } /// /// 锁定库存 /// [SugarColumn(Length = 18, DecimalDigits = 4, DefaultValue = "0")] public decimal LockQty { get; set; } } /// /// 物料属性 /// public class ItemInfoProperty { /// /// 库存Id /// [SugarColumn(IsPrimaryKey = true, IsIdentity = false, IsNullable = false)] public long StockId { get; set; } /// /// 物料Id /// [SugarColumn(IsNullable = false)] public long ItemId { get; set; } [Navigate(NavigateType.OneToOne, nameof(ItemId))] public ItemInfos ItemInfo { get; set; } /// /// 助记码 /// [SugarColumn(Length = 50, IsNullable = true)] public string Code { get; set; } /// /// 颜色 /// [SugarColumn(Length = 50, IsNullable = false)] public string Color { get; set; } /// /// 状态 /// [SugarColumn(DefaultValue = "1")] public bool Enabled { get; set; } = true; [JsonIgnore] [Navigate(NavigateType.OneToMany, nameof(MaterialStocks.StockId), nameof(StockId))] public List Stock { get; set; } } /// /// 物料档案 /// public class ItemInfos { [SugarColumn(IsPrimaryKey = true, IsIdentity = false, IsNullable = false)] public long Id { get; set; } /// /// 物料分类 /// [SugarColumn(DefaultValue = "0")] public long CategoryId { get; set; } /// /// 默认仓库 /// [SugarColumn(DefaultValue = "0")] public long WarehouseId { get; set; } /// /// 模具编码 /// [SugarColumn(IsNullable = true)] public string MouldCode { get; set; } /// /// 物料编码 /// [SugarColumn(IsNullable = true)] public string MaterialCode { get; set; } /// /// 物料名称 /// [SugarColumn(IsNullable = true)] public string MaterialName { get; set; } /// /// 零件单位 /// [SugarColumn(IsNullable = true)] public string MaterialUnit { get; set; } /// /// 价格 /// [SugarColumn(IsNullable = true, DefaultValue = "0", Length = 18, DecimalDigits = 6)] public decimal MaterialPrice { get; set; } /// /// 单重(g) /// [SugarColumn(IsNullable = true)] public string MaterialWeight { get; set; } /// /// 物料描述 /// [SugarColumn(ColumnDataType = "NVARCHAR(255)", IsNullable = true)] public string MaterialSpec { get; set; } /// /// 出模数量 /// [SugarColumn(IsNullable = true)] public string MoldRate { get; set; } /// /// 图片(支持多张图片,分号隔开) /// [SugarColumn(Length = 2000, IsNullable = true)] public string ProductImages { get; set; } /// /// 默认供应商 /// [SugarColumn(DefaultValue = "0")] public long SupplierId { get; set; } /// /// 是否通用件 /// [SugarColumn(DefaultValue = "0")] public bool IsCommon { get; set; } /// /// 是否纸箱 /// [SugarColumn(DefaultValue = "0")] public bool IsPaperBox { get; set; } /// /// 物料属性 /// [Navigate(NavigateType.OneToMany, nameof(ItemInfoProperty.ItemId))] public List Properties { get; set; } [Navigate(NavigateType.OneToOne, nameof(Id), nameof(MaterialStocks.ItemId))] public MaterialStocks Stock { get; set; } } [SugarTable("unitOrdersadsfaf")] public class Orders { [SugarColumn(IsPrimaryKey = true)] public long Id { get; set; } public long ItemId { get; set; } public long StockId { get; set; } [Navigate(NavigateType.OneToOne, nameof(StockId))] public ItemInfoProperty ItemProperty { get; set; } } public class View { public ItemInfoProperty ItemInfoProperty { get; set; } } }