如何验证 EF4.2 中不重叠的子实体时间段?
我已经使用我的 MVC3 站点设置了 EF4.2(数据库优先)。我必须实体固定装置和固定装置周期。赛程周期有开始日期和结束日期。
我需要允许客户在我的 MVC3 站点的单个视图中添加/编辑赛程周期。
我有这个工作。我还设法验证固定周期开始/结束日期的顺序是否正确。我在实现 IValidateObject 的固定周期的部分类中做到了这一点。然而现在我需要验证固定周期本身不会相互重叠。
我应该如何确保期间不重叠?我应该去哪里检查这个?我打算在 ViewModel 级别执行此操作(这看起来很简单),但这似乎是域级别的问题。它应该在实体、存储库、服务层中吗?
有什么建议吗?
谢谢
I have setup EF4.2 (Database First) with my MVC3 site. I have to entities fixture and fixture period. Fixture periods have start and end dates.
I need to allow customer to add/edit fixture periods in a single view in my MVC3 site.
I have this working. I also managed to validate that fixture period start/end dates are in the right order. I did that in a partial class of the fixture period implemetimng IValidateObject. However now I need to validate that the fixture periods themselves don't overlap with each other.
How should I ensure periods don't overlap? Where should I check for this? I was going to do it at the ViewModel level (which seemed easy) however it seems a domain level concern. Should it be in the entity, repository, a service layer?
Any advice?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是在数据库中创建一个用于插入和更新的后触发器。如果行重叠,那么我会引发错误并回滚插入。然后,我在对象上下文的 SaveChanges 上捕获了 SQL 异常,并通过向 ModelState 添加错误来进行处理。
Answer was to create an after trigger in the database for insert and update. If the rows overlapped then I raised an error and rolled back insert. I then caught the SQL Exception on SaveChanges on Object Context and handled by adding an error to ModelState.