LightSwitch - Business Rules
I have a Master/Detail screen in LightSwitch. I allow user to delete details' items. However, the user i开发者_开发问答s allowed to delete all except one. In other words,only one detail item needs to be present.
Is this doable in lightswitch?
Regards
Here's how to do this:
- Open the entity for which you want to restrict the delete operation in the LightSwitch designer
- Using the Write Code drop-down, create an Entity_Deleting event
- In the deleting event, check the count of detail items for the master entity. If it is 0 discard the changes.
Here's how to code this. In this example, the master entity is AdAgreement, and the detail entity is Issues:
partial void Issues_Deleting(Issue entity)
{
if (entity.Details.EntityState == EntityState.Deleted
&& entity.AdAgreement.Issues.Count() == 0)
{
entity.Details.DiscardChanges();
}
}
Unfortunately you can't perform this check in the Entity_CanDelete event, which would give you a better user experience, in my opinion. The problem is that CanDelete operates at the entity set level, and there is no access to the individual entity that would be deleted.
精彩评论