mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Update Demo
This commit is contained in:
parent
8df424dbb9
commit
5c22af3b2e
@ -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<MyCustomAttributeTable>();//Create Table
|
||||
|
||||
db.Insertable(new MyCustomAttributeTable() { Id = Guid.NewGuid().ToString(), Name = "Name" }).ExecuteCommand();
|
||||
var list = db.Queryable<MyCustomAttributeTable>().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();
|
||||
|
11
Src/Asp.Net/SqlServerTest/Demo/Demo2_Queryable.cs
Normal file
11
Src/Asp.Net/SqlServerTest/Demo/Demo2_Queryable.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OrmTest.Demo
|
||||
{
|
||||
class Demo2_Queryable
|
||||
{
|
||||
}
|
||||
}
|
20
Src/Asp.Net/SqlServerTest/Models/Class1.cs
Normal file
20
Src/Asp.Net/SqlServerTest/Models/Class1.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -49,6 +49,8 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Demo\Demo2_Queryable.cs" />
|
||||
<Compile Include="Models\Class1.cs" />
|
||||
<Compile Include="Models\Order.cs" />
|
||||
<Compile Include="Models\OrderItem.cs" />
|
||||
<Compile Include="Demo\Demo1_SqlSugarClient.cs" />
|
||||
|
BIN
Src/Asp.Net/SqlSugar/4.9.9.11 net 版本DLL .rar
Normal file
BIN
Src/Asp.Net/SqlSugar/4.9.9.11 net 版本DLL .rar
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user