diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs index 31cec4bcc..a0f120b3b 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Data.Linq.Mapping; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,8 +15,50 @@ namespace OrmTest { SingletonPattern();//单例 DistributedTransactionExample();//分布式事务 + CustomAttribute();//自定义特性 } + private static void CustomAttribute() + { + Console.WriteLine(""); + Console.WriteLine("#### Custom Attribute Start ####"); + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + ConfigureExternalServices = new ConfigureExternalServices() + { + EntityService = (property, column) => + { + + 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; + } + } + } + }); + db.CodeFirst.InitTables();//Create Table + + db.Insertable(new MyCustomAttributeTable() { Id = Guid.NewGuid().ToString(), Name = "Name" }).ExecuteCommand(); + var list = db.Queryable().ToList(); + + Console.WriteLine("#### Custom Attribute End ####"); + } + + private static void SingletonPattern() { Console.WriteLine(""); @@ -65,6 +109,12 @@ namespace OrmTest new ConnectionConfig(){ ConfigId=2, DbType=DbType.MySql, ConnectionString=Config.ConnectionString4 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true} }); + db.MappingTables.Add(typeof(Order).Name, typeof(Order).Name + "2018"); + db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order)); + + db.MappingTables.Add(typeof(Order).Name, typeof(Order).Name + "2019"); + db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order));// + //use first(SqlServer) db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Order), typeof(OrderItem));// db.Insertable(new Order() { Name = "order1", CreateTime = DateTime.Now }).ExecuteCommand(); diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo2_Queryable.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo2_Queryable.cs new file mode 100644 index 000000000..288f6defb --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo2_Queryable.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace OrmTest.Demo +{ + class Demo2_Queryable + { + } +} diff --git a/Src/Asp.Net/SqlServerTest/Models/Class1.cs b/Src/Asp.Net/SqlServerTest/Models/Class1.cs new file mode 100644 index 000000000..7bcf88b79 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/Models/Class1.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Data.Linq.Mapping; +using System.Linq; +using System.Text; + +namespace OrmTest +{ + [Table(Name = "CustomAttributeTable")] + //[SugarTable("CustomAttributeTable")] + public class MyCustomAttributeTable + { + + [Key] + //[SugarColumn(IsPrimaryKey =true)] + public string Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 6707bee85..c40171580 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -49,6 +49,8 @@ + + diff --git a/Src/Asp.Net/SqlSugar/4.9.9.11 net 版本DLL .rar b/Src/Asp.Net/SqlSugar/4.9.9.11 net 版本DLL .rar new file mode 100644 index 000000000..6243c1278 Binary files /dev/null and b/Src/Asp.Net/SqlSugar/4.9.9.11 net 版本DLL .rar differ