个人的CodeSmith和.NetTiers的学习心得及经验总结
1. 在学习过程中参考了这篇文章:《NetTier模板生成的代码框架用法》(http://www.cnblogs.com/minwellptl/articles/481208.html)
里面有这样一段例子:
1.3.6. 关联保存
深度保存,可以同时保存父对象和子集
/*
DeepSave helper method can help you to save an object and its children in
one call.
*/
using Northwind.DataAccessLayer;
Order order = Order.CreateOrder("ALFKI", 1, DateTime.Now, DateTime.Now,
DateTime.Now, 1, 0.1m, "ship name", "ship address" , "paris", "idf", "75000",
"france");
order.OrderDetailCollection.Add(order.OrderID, 1, 15.6m, 10, 0.02f);
order.OrderDetailCollection.Add(order.OrderID, 2, 122.6m, 43, 0.03f);
DataRepository.OrderProvider.DeepSave(order);
Console.WriteLine("new order saved: orderId is: " + order.OrderID.ToString());
从文章内容来看应该是很早的使用方式了,所以我改成了目前的2.3.1的方式,像这样:
Orders orders = Orders.CreateOrders("ALFKI", 1, DateTime.Now, DateTime.Now,
DateTime.Now, 1, 0.1m, "ship name", "ship address", "paris", "idf", "75000",
"france");
OrderDetails orderDetails = new Entities.OrderDetails();
orderDetails.OrderId = order.OrderID;
orderDetails.ProductId = 1;
orderDetails.UnitPrice = 15.6m;
orderDetails.Quantity = 10;
orderDetails.Discount = 0.02f;
orders.OrderDetailsCollection.Add(orderDetails);
orderDetails = new Entities.OrderDetails();
orderDetails.OrderId = order.OrderID;
orderDetails.ProductId = 2;
orderDetails.UnitPrice = 122.6m;
orderDetails.Quantity = 43;
orderDetails.Discount = 0.03f;
orders.OrderDetailsCollection.Add(orderDetails);
//orders.OrderDetailsCollection.Add(orders.OrderId, 1, 15.6m, 10, 0.02f));
//orders.OrderDetailsCollection.Add(orders.OrderId, 2, 122.6m, 43, 0.03f);
DataRepository.OrdersProvider.DeepSave(orders);
Console.WriteLine("new order saved: orderId is: " + orders.OrderId.ToString());
但是执行起来老是提示“INSERT 语句与 FOREIGN KEY 约束"FK_Order_Details_Orders"冲突。该冲突发生于数据库"Northwind",表"dbo.Orders", column 'OrderID'。语句已终止。”
查了一下,确实有这么个外键,而且OrderId也确实是零(没办法啊,赋值的时候确实是0啊),就去搜了一下,居然找不到一个有用的链接,
没办法只能自己琢磨了,于是从DeepSave开始跟踪,跟到这段代码:
if (CanDeepSave(entity.OrderDetailsCollection, "List<OrderDetails>|OrderDetailsCollection", deepSaveType, innerList))
{
// update each child parent id with the real parent id (mostly used on insert)
foreach(OrderDetails child in entity.OrderDetailsCollection)
{
if(child.OrderIdSource != null)
{
child.OrderId = child.OrderIdSource.OrderId;
}
if(child.ProductIdSource != null)
{
child.ProductId = child.ProductIdSource.ProductId;
}
}
if (entity.OrderDetailsCollection.Count > 0 || entity.OrderDetailsCollection.DeletedItems.Count > 0)
{
//DataRepository.OrderDetailsProvider.Save(transactionManager, entity.OrderDetailsCollection);
deepHandles.Add("OrderDetailsCollection",
new KeyValuePair<Delegate, object>((DeepSaveHandle< OrderDetails >) DataRepository.OrderDetailsProvider.DeepSave,
new object[] { transactionManager, entity.OrderDetailsCollection, deepSaveType, childTypes, innerList }
));
}
}
O了,要的就是它,改成这样就OK了:
orderDetails.OrderIdSource = orders;
BTW:为什么没有人碰到这个问题呢?奇怪啊,难道是.NetTiers太新了,没人用这个版本?但是2.3.1也发布了有一年多了啊,没人用总有人学吧,
也没人提这个问题啊,不管了,放在这儿,立此存照吧,也希望能帮到有缘人。