如何使用 crm 2011 sdk 和 XrmServiceContext 创建事务?
在下一个示例中,“new_brand”是一些自定义实体。我想创建三个品牌。第三个有错误的 OwnerID 指导。当我调用 SaveChanges() 方法时,创建了两个品牌并且我遇到了异常。如何回滚创建前两个品牌?
是否可以不使用插件和工作流程?
using (var context = new XrmServiceContext(connection))
{
SystemUser owner = context.SystemUserSet.FirstOrDefault(s => s.Id == new Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"));
// create 3 brands
new_brand b1 = new new_brand();
b1.new_brandidentification = 200;
b1.new_name = "BRAND 200";
b1.OwnerId = owner.ToEntityReference();
context.AddObject(b1);
new_brand b2 = new new_brand();
b2.new_brandidentification = 300;
b2.new_name = "BRAND 300";
b2.OwnerId = owner.ToEntityReference();
context.AddObject(b2);
new_brand b3 = new new_brand();
b3.new_brandidentification = 400;
b3.new_name = "BRAND 400";
b3.OwnerId = new EntityReference(SystemUser.EntityLogicalName, new Guid("00000000-0000-0000-0000-000000000000"));
context.AddObject(b3);
context.SaveChanges();
}