虽然不完全符合您的要求,但您可以通过 API 实现类似的效果。在预验证阶段编写一个针对Create
and的插件(以在数据库事务之前捕获值)。如果不可为空的属性永远为空,则抛出异常,这将完全取消事务。Update
null
如果您在创建记录后注册插件,那么您只需要确保使用值填充那些预先存在的记录。
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
var target = ((Entity)context.InputParameters["Target"]);
if (target.LogicalName == Appointment.EntityLogicalName)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
var entity = target.ToEntity<Xrm.Appointment>();
if (entity.Description == null && entity.Attributes.Contains("description"))
{
throw new Microsoft.Xrm.Sdk.SaveChangesException("Fill in the field, buddy.");
}
}
}
}