如何使用Silverlight + Domain Service + EF
执行约束验证表1是主表
表1是表2中的FK
我需要在删除Table1时进行验证,表2没有与Table1相关的记录。
I Table2有相关记录然后抛出异常。
有可能吗?
答案 0 :(得分:1)
在您的DomainService中,只需在删除方法
上添加验证属性即可[CustomValidation(typeof(MyCustomValidator), "CustomDeleteTable1Validation")]
public void DeleteTable1(Table1 table1)
{
//normal behavior
if ((table1.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(table1, EntityState.Deleted);
}
else
{
this.ObjectContext.Table1s.Attach(table1);
this.ObjectContext.Table1s.DeleteObject(table1);
}
}
然后在验证方法中执行任何操作
public static class MyCustomValidator
{
public static ValidationResult CustomDeleteTable1Validation(Table1 table1, ValidationContext context)
{
// check your values ...
var isOk=true;
// and ...
if(isOk)
return ValidationResult.Success;
else
return new ValidationResult(validationContext.DisplayName + " error");
}
}
享受!