From cb366cc7719a0a8d12f36aaac54f56737ca14f6c Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sun, 22 Mar 2020 01:15:40 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20Navigate=20=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=9C=AA=E8=AE=BE=E7=BD=AE=20set=20=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=8F=8B=E5=A5=BD=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=EF=BC=9B?= =?UTF-8?q?=20-=20=E5=A2=9E=E5=8A=A0=20=E5=BB=B6=E6=97=B6=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=87=8D=E5=86=99=E7=B1=BB=E5=AF=B9=20protected=20set?= =?UTF-8?q?=20=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 3 + Examples/base_entity/Test01/Role.cs | 52 ++++++++ Examples/base_entity/Test01/User.cs | 154 ++++++++++++++++++++++ Examples/base_entity/Test01/UserRole.cs | 36 ++++++ Examples/base_entity/base_entity.xml | 165 ++++++++++++++++++++++++ FreeSql/Internal/UtilsExpressionTree.cs | 34 +++-- 6 files changed, 432 insertions(+), 12 deletions(-) create mode 100644 Examples/base_entity/Test01/Role.cs create mode 100644 Examples/base_entity/Test01/User.cs create mode 100644 Examples/base_entity/Test01/UserRole.cs diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index 146f9603..5cb92a98 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -49,6 +49,9 @@ namespace base_entity BaseEntity.Initialization(fsql); #endregion + var test01 = EMSServerModel.Model.User.Select.IncludeMany(a => a.Roles).ToList(); + var test01tb = EMSServerModel.Model.User.Orm.CodeFirst.GetTableByEntity(typeof(EMSServerModel.Model.User)); + var us = User1.Select.Limit(10).ToList(); new Products { title = "product-1" }.Save(); diff --git a/Examples/base_entity/Test01/Role.cs b/Examples/base_entity/Test01/Role.cs new file mode 100644 index 00000000..2db80d33 --- /dev/null +++ b/Examples/base_entity/Test01/Role.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json; +using FreeSql.DataAnnotations; +using FreeSql; + +namespace EMSServerModel.Model +{ + /// + /// 角色表 + /// + [JsonObject(MemberSerialization.OptIn)] + public partial class Role : BaseEntity{ + /// + /// 角色编号 + /// + [JsonProperty, Column(IsPrimary = true, IsIdentity = true)] + public long RoleId { get; set; } + + /// + /// 角色名称 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string RoleName { get; set; } = string.Empty; + + /// + /// 角色描述 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string RoleDesc { get; set; } = string.Empty; + + ///// + ///// 创建时间 + ///// + //[JsonProperty, Column(DbType = "date")] + //public DateTime CreateTime { get; set; } = DateTime.Now; + + /// + /// 启用 + /// + [JsonProperty] + public bool IsEnable { get; set; } = true; + + /// + /// 角色用户多对多导航 + /// + [Navigate(ManyToMany = typeof(UserRole))] + public virtual ICollection Users { get; protected set; } + + } + +} diff --git a/Examples/base_entity/Test01/User.cs b/Examples/base_entity/Test01/User.cs new file mode 100644 index 00000000..46dc8e95 --- /dev/null +++ b/Examples/base_entity/Test01/User.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json; +using FreeSql.DataAnnotations; +using System.ComponentModel.DataAnnotations; +using FreeSql; + +namespace EMSServerModel.Model +{ + /// + /// 鐢ㄦ埛琛 + /// + [JsonObject(MemberSerialization.OptIn)] + public partial class User : BaseEntity{ + + //[JsonProperty, Column(IsIdentity = true)] + //public long Id { get; set; } + + /// + /// 缂栧彿 + /// + [JsonProperty, Column(DbType = "varchar(50)", IsPrimary = true)] + public string UserId { get; set; } = string.Empty; + + /// + /// 澶村儚 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Avatar { get; set; } = string.Empty; + + /// + /// 濮撳悕 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string UserName { get; set; } = string.Empty; + + /// + /// 鑹哄悕 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string NickName { get; set; } = string.Empty; + + /// + /// 鐢佃瘽 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Tel { get; set; } = string.Empty; + + /// + /// 鎬у埆 + /// + [JsonProperty] + public Sex Sex { get; set; } = Sex.鐢; + + /// + /// 璇佷欢鍙 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string UID { get; set; } = string.Empty; + + /// + /// 鐢熸棩 + /// + [JsonProperty, Column(DbType = "date")] + public DateTime? DateOfBirth { get; set; } + + + /// + /// 鍑虹敓鍦 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string PlaceOfBirth { get; set; } = string.Empty; + + /// + /// 灞呬綇鍦 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Addr { get; set; } = string.Empty; + + + /// + /// 瀵嗙爜 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Pwd { get; set; } = string.Empty; + + + /// + /// 閮ㄩ棬缂栧彿 + /// + [JsonProperty] + public long? DeptId { get; set; } + + /// + /// 鑱屽姟缂栧彿 + /// + [JsonProperty] + public long? TitleId { get; set; } + + + ///// + ///// 鍒涘缓鏃堕棿 + ///// + //[JsonProperty, Column(DbType = "date")] + //public DateTime CreateTime { get; set; } = DateTime.Now; + + /// + /// 鍥界睄 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Nationality { get; set; } = string.Empty; + + /// + /// 缁忔墜浜 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string Handler { get; set; } = string.Empty; + + /// + /// 鍚敤 + /// + [JsonProperty] + public bool IsEnable { get; set; } = true; + + + /// + /// 澶囨敞 + /// + [JsonProperty, Column(DbType = "varchar(100)")] + public string Memos { get; set; } + + /// + /// + /// + [Navigate(ManyToMany = typeof(UserRole))] + public virtual ICollection Roles { get; protected set; } + + } + /// + /// 鎬у埆鏋氫妇 + /// + public enum Sex + { + /// + /// 濂=0 + /// + 濂=0, + /// + /// 鐢=1 + /// + 鐢=1 + } + +} diff --git a/Examples/base_entity/Test01/UserRole.cs b/Examples/base_entity/Test01/UserRole.cs new file mode 100644 index 00000000..3c13335a --- /dev/null +++ b/Examples/base_entity/Test01/UserRole.cs @@ -0,0 +1,36 @@ +using Newtonsoft.Json; +using FreeSql.DataAnnotations; +using FreeSql; + +namespace EMSServerModel.Model +{ + /// + /// 用户角色关系表 + /// + [JsonObject(MemberSerialization.OptIn)] + public partial class UserRole : BaseEntity{ + /// + /// 角色编号 + /// + [JsonProperty] + public long RoleId { get; set; } + /// + /// 角色导航 + /// + [Navigate("RoleId")] + public Role Roles { get; set; } + + /// + /// 用户编号 + /// + [JsonProperty, Column(DbType = "varchar(50)")] + public string UserId { get; set; } + /// + /// 用户导航 + /// + [Navigate("UserId")] + public User Users { get; set; } + + } + +} diff --git a/Examples/base_entity/base_entity.xml b/Examples/base_entity/base_entity.xml index 682b4b8a..a2fd01f9 100644 --- a/Examples/base_entity/base_entity.xml +++ b/Examples/base_entity/base_entity.xml @@ -29,5 +29,170 @@ 鎻忚堪 + + + 瑙掕壊琛 + + + + + 瑙掕壊缂栧彿 + + + + + 瑙掕壊鍚嶇О + + + + + 瑙掕壊鎻忚堪 + + + + + 鍚敤 + + + + + 瑙掕壊鐢ㄦ埛澶氬澶氬鑸 + + + + + 鐢ㄦ埛琛 + + + + + 缂栧彿 + + + + + 澶村儚 + + + + + 濮撳悕 + + + + + 鑹哄悕 + + + + + 鐢佃瘽 + + + + + 鎬у埆 + + + + + 璇佷欢鍙 + + + + + 鐢熸棩 + + + + + 鍑虹敓鍦 + + + + + 灞呬綇鍦 + + + + + 瀵嗙爜 + + + + + 閮ㄩ棬缂栧彿 + + + + + 鑱屽姟缂栧彿 + + + + + 鍥界睄 + + + + + 缁忔墜浜 + + + + + 鍚敤 + + + + + 澶囨敞 + + + + + + + + + + 鎬у埆鏋氫妇 + + + + + 濂=0 + + + + + 鐢=1 + + + + + 鐢ㄦ埛瑙掕壊鍏崇郴琛 + + + + + 瑙掕壊缂栧彿 + + + + + 瑙掕壊瀵艰埅 + + + + + 鐢ㄦ埛缂栧彿 + + + + + 鐢ㄦ埛瀵艰埅 + + diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index cf78e9a3..d9dc1840 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -66,7 +66,7 @@ namespace FreeSql.Internal trytb.DbOldName = trytb.DbOldName?.ToUpper(); } if (tbattr != null) trytb.DisableSyncStructure = tbattr.DisableSyncStructure; - var propsLazy = new List>(); + var propsLazy = new List>(); var propsNavObjs = new List(); var propsComment = CommonUtils.GetProperyCommentBySummary(entity); var columnsList = new List(); @@ -79,6 +79,9 @@ namespace FreeSql.Internal { if (colattr == null) colattr = new ColumnAttribute { IsIgnore = true }; else colattr.IsIgnore = true; + //Navigate 閿欒鎻愮ず + var pnvAttr = common.GetEntityNavigateAttribute(trytb.Type, p); + if (pnvAttr != null) throw new Exception($"銆愬鑸睘鎬с憑trytb.Type.DisplayCsharp()}.{p.Name} 缂哄皯 set 灞炴"); } if (tp == null && colattr?.IsIgnore != true) { @@ -88,7 +91,7 @@ namespace FreeSql.Internal var getIsVirtual = getMethod?.IsVirtual == true && getMethod?.IsFinal == false;// trytb.Type.GetMethod($"get_{p.Name}")?.IsVirtual; var setIsVirtual = setMethod?.IsVirtual == true && setMethod?.IsFinal == false; if (getIsVirtual == true || setIsVirtual == true) - propsLazy.Add(NaviteTuple.Create(p, getIsVirtual, setIsVirtual)); + propsLazy.Add(NaviteTuple.Create(p, getIsVirtual, setIsVirtual, getMethod, setMethod)); } propsNavObjs.Add(p); continue; @@ -446,10 +449,17 @@ namespace FreeSql.Internal return tbc.TryGetValue(entity, out var trytb2) ? trytb2 : trytb; } - public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo pnv, bool isLazy, NaviteTuple vp, StringBuilder cscode) + public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo pnv, bool isLazy, NaviteTuple vp, StringBuilder cscode) { + var getMethod = vp?.Item4; + var setMethod = vp?.Item5; var trytbTypeName = trytb.Type.DisplayCsharp(); var propTypeName = pnv.PropertyType.DisplayCsharp(); + var propModification = (getMethod?.IsPublic == true || setMethod?.IsPublic == true ? "public " : (getMethod?.IsAssembly == true || setMethod?.IsAssembly == true ? "internal " : (getMethod?.IsFamily == true || setMethod?.IsFamily == true ? "protected " : (getMethod?.IsPrivate == true || setMethod?.IsPrivate == true ? "private " : "")))); + var propSetModification = (setMethod?.IsPublic == true ? "public " : (setMethod?.IsAssembly == true ? "internal " : (setMethod?.IsFamily == true ? "protected " : (setMethod?.IsPrivate == true ? "private " : "")))); + var propGetModification = (getMethod?.IsPublic == true ? "public " : (getMethod?.IsAssembly == true ? "internal " : (getMethod?.IsFamily == true ? "protected " : (getMethod?.IsPrivate == true ? "private " : "")))); + if (propSetModification == propModification) propSetModification = ""; + if (propGetModification == propModification) propGetModification = ""; var pnvAttr = common.GetEntityNavigateAttribute(trytb.Type, pnv); var pnvBind = pnvAttr?.Bind?.Split(',').Select(a => a.Trim()).Where(a => !string.IsNullOrEmpty(a)).ToArray(); @@ -788,10 +798,10 @@ namespace FreeSql.Internal if (isLazy) { cscode.Append(" private bool __lazy__").Append(pnv.Name).AppendLine(" = false;") - .Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); + .Append(" ").Append(propModification).Append(" override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); if (vp?.Item2 == true) { //get 閲嶅啓 - cscode.Append(" get {\r\n") + cscode.Append(" ").Append(propGetModification).Append(" get {\r\n") .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); if (nvref.Exception == null) @@ -808,7 +818,7 @@ namespace FreeSql.Internal } if (vp?.Item3 == true) { //set 閲嶅啓 - cscode.Append(" set {\r\n") + cscode.Append(" ").Append(propSetModification).Append(" set {\r\n") .Append(" base.").Append(pnv.Name).AppendLine(" = value;") .Append(" }\r\n"); } @@ -923,10 +933,10 @@ namespace FreeSql.Internal if (isLazy) { cscode.Append(" private bool __lazy__").Append(pnv.Name).AppendLine(" = false;") - .Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); + .Append(" ").Append(propModification).Append(" override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); if (vp?.Item2 == true) { //get 閲嶅啓 - cscode.Append(" get {\r\n") + cscode.Append(" ").Append(propGetModification).Append(" get {\r\n") .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); if (nvref.Exception == null) @@ -949,7 +959,7 @@ namespace FreeSql.Internal } if (vp?.Item3 == true) { //set 閲嶅啓 - cscode.Append(" set {\r\n") + cscode.Append(" ").Append(propSetModification).Append(" set {\r\n") .Append(" base.").Append(pnv.Name).AppendLine(" = value;") .Append(" }\r\n"); } @@ -1079,10 +1089,10 @@ namespace FreeSql.Internal if (isLazy) { cscode.Append(" private bool __lazy__").Append(pnv.Name).AppendLine(" = false;") - .Append(" public override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); + .Append(" ").Append(propModification).Append(" override ").Append(propTypeName).Append(" ").Append(pnv.Name).AppendLine(" {"); if (vp?.Item2 == true) { //get 閲嶅啓 - cscode.Append(" get {\r\n") + cscode.Append(" ").Append(propGetModification).Append(" get {\r\n") .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); if (nvref.Exception == null) @@ -1098,7 +1108,7 @@ namespace FreeSql.Internal } if (vp?.Item3 == true) { //set 閲嶅啓 - cscode.Append(" set {\r\n") + cscode.Append(" ").Append(propSetModification).Append(" set {\r\n") .Append(" base.").Append(pnv.Name).AppendLine(" = value;") .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;") .Append(" }\r\n");