插入数据
使用 SQL 语句:
1: int productId = Context.Sql(@"insert into Product(Name, CategoryId)
2: values(@0, @1);")
3: .Parameters("The Warren Buffet Way", 1)
4: .ExecuteReturnLastId<int>();
使用builder:
1: int productId = Context.Insert("Product")
2: .Column("Name", "The Warren Buffet Way")
3: .Column("CategoryId", 1)
4: .ExecuteReturnLastId<int>();
使用builder,并且自动映射
1: Product product = new Product();
2: product.Name = "The Warren Buffet Way";
3: product.CategoryId = 1;
4:
5: product.ProductId = Context.Insert<Product>("Product", product)
6: .AutoMap(x => x.ProductId)
7: .ExecuteReturnLastId<int>();
8:
将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它是一个数据库自增长字段。
更新数据
使用SQL语句:
1: int rowsAffected = Context.Sql(@"update Product set Name = @0
2: where ProductId = @1")
3: .Parameters("The Warren Buffet Way", 1)
4: .Execute();
使用builder:
1: int rowsAffected = Context.Update("Product")
2: .Column("Name", "The Warren Buffet Way")
3: .Where("ProductId", 1)
4: .Execute();
使用builder,并且自动映射:
1: Product product = Context.Sql(@"select * from Product
2: where ProductId = 1")
3: .QuerySingle<Product>();
4: product.Name = "The Warren Buffet Way";
5:
6: int rowsAffected = Context.Update<Product>("Product", product)
7: .AutoMap(x => x.ProductId)
8: .Where(x => x.ProductId)
9: .Execute();
将ProductId作为AutoMap方法的参数,是要指明ProductId不需要进行映射,因为它不需要被更新。
Insert and update - common Fill method
1: var product = new Product();
2: product.Name = "The Warren Buffet Way";
3: product.CategoryId = 1;
4:
5: var insertBuilder = Context.Insert<Product>("Product", product).Fill(FillBuilder);
6:
7: var updateBuilder = Context.Update<Product>("Product", product).Fill(FillBuilder);
8:
9: public void FillBuilder(IInsertUpdateBuilder<Product> builder)
10: {
11: builder.Column(x => x.Name);
12: builder.Column(x => x.CategoryId);
13: }
14:
15: Delete
删除数据
使用SQL语句:
1: int rowsAffected = Context.Sql(@"delete from Product
2: where ProductId = 1")
3: .Execute();
使用builder:
1: int rowsAffected = Context.Delete("Product")
2: .Where("ProductId", 1)
3: .Execute();
Insert data
Using SQL:
int productId = Context.Sql(@"insert into Product(Name, CategoryId)
values(@0, @1);")
.Parameters("The Warren Buffet Way", 1)
.ExecuteReturnLastId<int>();
Using a builder:
int productId = Context.Insert("Product")
.Column("Name", "The Warren Buffet Way")
.Column("CategoryId", 1)
.ExecuteReturnLastId<int>();
Using a builder with automapping:
Product product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
product.ProductId = Context.Insert<Product>("Product", product)
.AutoMap(x => x.ProductId)
.ExecuteReturnLastId<int>();
We send in ProductId to the AutoMap method to get AutoMap to ignore and not map the ProductId since this property is an identity field where the value is generated in the database.
Update data
Using SQL:
int rowsAffected = Context.Sql(@"update Product set Name = @0
where ProductId = @1")
.Parameters("The Warren Buffet Way", 1)
.Execute();
Using a builder:
int rowsAffected = Context.Update("Product")
.Column("Name", "The Warren Buffet Way")
.Where("ProductId", 1)
.Execute();
Using a builder with automapping:
Product product = Context.Sql(@"select * from Product
where ProductId = 1")
.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
int rowsAffected = Context.Update<Product>("Product", product)
.AutoMap(x => x.ProductId)
.Where(x => x.ProductId)
.Execute();
We send in ProductId to the AutoMap method to get AutoMap to ignore and not map the ProductId since this is the identity field that should not get updated.
Insert and update - common Fill method
var product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
var insertBuilder = Context.Insert<Product>("Product", product).Fill(FillBuilder);
var updateBuilder = Context.Update<Product>("Product", product).Fill(FillBuilder);
public void FillBuilder(IInsertUpdateBuilder<Product> builder)
{
builder.Column(x => x.Name);
builder.Column(x => x.CategoryId);
}
Delete data
Using SQL:
int rowsAffected = Context.Sql(@"delete from Product
where ProductId = 1")
.Execute();
Using a builder:
int rowsAffected = Context.Delete("Product")
.Where("ProductId", 1)
.Execute();