EF Code First - SqlServerCE 触发器
我首先为我的应用程序使用实体框架代码,并且我需要一个触发器。 我的应用程序应该支持不同的数据库引擎,例如
- Sql Server
- SqlServerCE
- SqlExpress
触发器,SqlServerCE 不支持存储过程,你会做什么才能得到这个 功能?
我可以在“SaveChanges”之后做一些事情,什么是好方法?
i use the entity framework code first for my application and i need a trigger.
My Application should support different database engines like
- Sql Server
- SqlServerCE
- SqlExpress
Trigger, Stored procs are not supported in SqlServerCE, what would you do to get this
functionality?
I could do something after "SaveChanges" or something, what is a good way ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以在 SaveChanges 内部(通过覆盖)或在 SaveChanges 之后执行某些操作并再次调用 SaveChanges 来保存新数据,但它与触发器不完全相同。简而言之,如果您的要求是使用触发器来实现某些功能,那么 SqlServerCE 不适合您。如果您将触发器逻辑重写到应用程序中,无论如何您都应该有两个版本 - 一种用于使用触发器的大型 SQL,另一种用于不使用触发器的 SQL CE。
Yes you can do something inside SaveChanges (by overriding) or after SaveChanges and call SaveChanges again to persist new data but it is not exactly the same as trigger. Simply if your requirement is to use trigger for some functionality SqlServerCE is not a choice for you. If you rewrite the trigger logic to your application you should have two versions anyway - one for big SQL using triggers and one for SQL CE not using triggers.
代码优先,虽然它允许您发送一些原始查询并因此执行您自己的本机数据库/服务器操作,但基本上仅设计用于构建模型并查询该模型。
现在关于您的问题:您可以通过向 DbContext 添加方法(或用于分离关注点的扩展方法)来直接构建“存储过程替代方案”。
触发器稍微复杂一些。您可以重写 SaveChanges 方法,在该方法中您基本上可以跟踪在持久化回数据库之前或之后所做的每个更改。
然而,这两个问题也可以通过引入存储库来解决。请参阅:http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1 这允许您在添加、更新或删除某个实体时启动脚本(触发器)
Code first, although it allows you to send in some raw queries and hence perform your own native database/server manipulations, basically is designed only for building a model and querying upon that model.
Now as for your question: You can directly build 'stored procedure alternatives' by adding methods to your DbContext (or extension methods for seperation of concerns).
Triggers are a little more complex. You can override the SaveChanges method in which you can basically track each change made either before it got persisted back to the database- or after.
Both problems however can also be solved by introducing a repository. See: http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1 This allows you to launch a script (trigger) when adding, updating or removing a certain entity