检查将 linq-to-sql 项目插入或提交到数据库的限制
我想在插入或提交对象到sql时实现对象数据的检查。是否有一些 OnInsert 或 OnSubmit 函数可以为自动生成的 linq-to-sql-classes 实现/扩展,或者其他解决方案?
执行此操作的正常方法是将限制、检查约束、触发器和存储过程放入 sql server 数据库中,但我想避免这种情况,以便将我的代码保留在一处并进行语法检查等
(我认为唯一索引和外键的检查最好直接在 sql 数据库中实现)
I would like to implement checking of object data when inserting or submitting the object to sql. Is there some OnInsert or OnSubmit function that I could implement/extend for the automatically generated linq-to-sql-classes, or some other solution?
The normal way to do this would be to put restrictions, check-constraints, triggers and stored procedures in the sql server db, but I would like to avoid that in order to keep my code in one place and get syntax checking etc.
(I think checking of unique indexes and foreign keys are better implemented directly in the sql db)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
OnValidate()
函数与 LINQ-to-SQL 实体的分部类一起使用。 OnValidate() 将在 SubmitChanges() 期间但在数据发送到数据库之前调用。有关... stackoverflow 的更多信息... 在 SubmitChanges() 之前验证 Linq2Sql
You can use the
OnValidate()
function with the LINQ-to-SQL Entity's partial class. OnValidate() will then be called during SubmitChanges() but before the data is sent to the database.More info on... stackoverflow.... Validate Linq2Sql before SubmitChanges()