diff --git a/Src/Asp.Net/SqlServerTest/Demos/H_ExtEntity.cs b/Src/Asp.Net/SqlServerTest/Demos/H_ExtEntity.cs index 7e140d7e2..af0c529af 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/H_ExtEntity.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/H_ExtEntity.cs @@ -1,11 +1,16 @@ using SqlSugar; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Data.Linq.Mapping; using System.Linq; using System.Text; namespace OrmTest.Demo { + /// + /// mapping ef attribute + /// public class ExtEntity: DemoBase { public static void Init() @@ -16,22 +21,36 @@ namespace OrmTest.Demo IsAutoCloseConnection = true, ConfigureExternalServices=new ConfigureExternalServices() { EntityService = (property, column) => { - if (property.Name == "xxx") { + if (property.Name == "xxx") {// by name ignore column column.IsIgnore = true; } - //property.GetCustomAttributes + var attributes = property.GetCustomAttributes(true);//get all attributes + + if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey + { + column.IsPrimarykey = true; + } + }, + EntityNameService = (type,entity) => { + var attributes = type.GetCustomAttributes(true); + if (attributes.Any(it => it is TableAttribute)) + { + entity.DbTableName = (attributes.First(it => it is TableAttribute)as TableAttribute).Name; + } } } }); - var sql=db.Queryable().ToSql(); - var sql2 = db.Insertable(new StudentTest()).ToSql(); + var sql=db.Queryable().ToList(); + var sql2 = db.Insertable(new StudentTest()).ExecuteCommand(); } } + [Table(Name ="student")]//default public class StudentTest { + [Key] public string Id { get; set; } public string xxx { get; set; } public string Name { get; set; } diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 7f80d23fa..6c5800ef5 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -39,7 +39,9 @@ OtherDll\SyntacticSugar.dll + + diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index cd2bc6605..fbf77e460 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -28,6 +28,9 @@ namespace SqlSugar var sugarTable = (SugarTable)sugarAttributeInfo; result.DbTableName = sugarTable.TableName; } + if (this.Context.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) { + this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService(type,result); + } result.Type = type; result.EntityName = result.Type.Name; result.Columns = new List(); diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index b06cb99b4..2c25c7172 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -94,5 +94,6 @@ namespace SqlSugar public Action EntityService{ get; set; } + public Action EntityNameService { get; set; } } }