public interface Category : Entity
{
[PrimaryKey]
int CategoryID { get; }
string CategoryName { get; set; }
[FkQuery("Category", Contained=true, LazyLoad=true)]
Product[] Products
{
get; set;
}
}
public interface Product : Entity
{
[PrimaryKey]
int ProductID { get; }
string ProductName { get; set; }
[FkReverseQuery(LazyLoad = true)]
Category Category
{
get; set;
}
}
=====insert===
Category category = new Category();
category.CategoryName = "11";
Product product = new Product();
product.ProductName = "1111";
ProductArrayList prarry = new ProductArrayList();
prarry.Add(product);
category.Products = prarry;
Gateway.Default.Save<Category>(category);
=====================执行结果正确,product表也被插入数据了
===update=======
Category category = new Category();
category.CategoryID=1;
category.Attach(); /////////////////////////////
(用find方法和手动效果一样)
category.CategoryName = "222222222";
Product product = new Product();
product.ProductName = "222222222222";
ProductArrayList prarry = new ProductArrayList();
prarry.Add(product);
category.Products = prarry;
Gateway.Default.Save<Category>(category);
=====================执行结果,只有category被更新
product表没有被更新
另:实际代码save使用
DbTransaction tran = Gateway.Default.BeginTransaction();
int ret = 0;
try
{
Gateway.Default.Save<Category>(category, tran);
//Gateway.Default.Save<Product>(product, tran);
tran.Commit();
}
catch
{
tran.Rollback();
}
finally
{
Gateway.Default.CloseTransaction(tran);
}