开发者

Conditional UPDATE in EF

I have relatio开发者_开发知识库nship 1 to many and want to check business rules in scope of update. For example, have the following situation: The first table contains groups and the second one contains students. Also there is a business rule which says: "a group could not contain more than 10 students" in our system. We need to check the rule in scope of update or transaction, because prevalidation in code does not guarantee fulfillment of the rule.

Is it possible to make UPDATE with WHERE condition without custom scripts in EF? What is the best way to make this kind of validation in EF?


No you cannot include such WHERE condition into UPDATE statements in EF. Moreover this is typical business rule validation which is little bit out of scope in the database layer. You should build a mechanism which will allow only single client modifying a group. The client will load a group (= lock) change the number of students and try to save the group. Because the group is exclusively locked by the single client nobody else could modify the content and you can freely check number of students before you save the group and release the lock. The simplest way to create such locking mechanism is adding columns LockedBy and LockedDate. To avoid unlimited locking you can have some condition that locks older then X are considered as expired.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜