SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.5 KiB

6 years ago
using SqlSugar;
using System;
6 years ago
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class Demo3_Insertable
{
6 years ago
public static void Init()
{
6 years ago
Console.WriteLine("");
Console.WriteLine("#### Insertable Start ####");
6 years ago
6 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
var insertObj = new Order() { Id = 1, Name = "order1", Price = 0 };
4 years ago
var insertObjs = new List<Order> {
6 years ago
new Order() { Id = 11, Name = "order11", Price=0 },
new Order() { Id = 12, Name = "order12" , Price=0}
6 years ago
};
6 years ago
//Ignore CreateTime
db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity
db.Insertable(insertObj).IgnoreColumns("CreateTime").ExecuteReturnIdentity();
6 years ago
//Only insert Name and Price
db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.Price }).ExecuteReturnIdentity();
6 years ago
db.Insertable(insertObj).InsertColumns("Name", "Price").ExecuteReturnIdentity();
6 years ago
//ignore null columns
4 years ago
db.Insertable(insertObjs).ExecuteCommand();//get change row count
6 years ago
//Use Lock
db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
4 years ago
insertObjs = new List<Order> {
new Order() { Id = 11, Name = "order11", Price=1 },
new Order() { Id = 12, Name = "order12" , Price=20, CreateTime=DateTime.Now, CustomId=1}
};
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
db.Insertable(new Order()
{
Name = "订单 1",
CustomId = 1,
Price = 100,
CreateTime = DateTime.Now,
Id = 0,
Items = new List<OrderItem>() {
new OrderItem(){
CreateTime=DateTime.Now,
OrderId=0,
Price=1,
ItemId=1
}
}
})
.AddSubList(it => it.Items.First().OrderId).ExecuteReturnPrimaryKey();
db.CodeFirst.InitTables<SubInsertTest, SubInsertTestItem, SubInsertTestItem1, SubInsertTestItem2>();
db.Insertable(new SubInsertTest()
{
Name="aa",
SubInsertTestItem1=new SubInsertTestItem1() {
a="nn"
},
SubInsertTestItem=new SubInsertTestItem()
{
Name ="item" ,
TestId=2
}
})
.AddSubList(it => it.SubInsertTestItem1)
.AddSubList(it => it.SubInsertTestItem.TestId)
.ExecuteReturnPrimaryKey();
6 years ago
Console.WriteLine("#### Insertable End ####");
6 years ago
}
}
}