实体框架与存储过程 [英] Entity Framework vs. stored procedures

查看:84
本文介绍了实体框架与存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



实体框架如何处理存储过程?我们有一个存储过程可以从数据库中删除一大堆数据,但它不会更新EF缓存。



我们的应用程序是数据库优先,这意味着我们通过从数据库更新模型来创建实体。从数据库更新模型还会在数据库上下文中创建方法以调用任何现有存储过程。例如,我们在数据库上下文中使用此方法结束
以调用DeleteProjectData,存储过程有问题:



Hello,

How does Entity Framework handle stored procedures? We have a stored procedure that deletes a whole bunch of data from the database but it doesn't update the EF cache.

Our application is database first, which means that we create our entities by updating the model from the database. Updating the model from the database also creates methods in the database context to call any existing stored procedures. For example, we end up with this method in the database context for calling DeleteProjectData, the stored procedure in question:

public virtual int DeleteProjectData(Nullable<int> projectId, string deleteType, string username)
        {
            var projectIdParameter = projectId.HasValue ?
                new ObjectParameter("projectId", projectId) :
                new ObjectParameter("projectId", typeof(int));
    
            var deleteTypeParameter = deleteType != null ?
                new ObjectParameter("deleteType", deleteType) :
                new ObjectParameter("deleteType", typeof(string));
    
            var usernameParameter = username != null ?
                new ObjectParameter("username", username) :
                new ObjectParameter("username", typeof(string));
    
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("DeleteProjectData", projectIdParameter, deleteTypeParameter, usernameParameter);
        }








调用此方法时,实体框架是否采用注意?也就是说,它是否知道存储过程被调用?如果是这样,在存储过程完成后,它如何确保缓存的数据仍然与数据库的状态同步?
它是否能够跟踪存储过程对数据库所做的更改?
b


如果没有,是否有办法制作实体框架了解这些变化并对其做些什么?




When this method is called, does Entity Framework take note? That is, is it aware that the stored procedure is called? If so, what does it do to ensure the data it has cached is still in sync with the state of the database after the stored procedure is complete? Is it even able to keep track of what changes the stored procedure is making to the database?

If not, is there a way to make Entity Framework aware of the changes and to do something about it?

推荐答案

调用此方法时,是否实体框架做记录?也就是说,它是否知道存储过程被调用?如果是这样,在
存储过程完成后,它如何确保缓存的数据仍然与数据库的状态同步?它是否能够跟踪存储过程对数据库所做的更改?


< br style ="">
如果没有,是否有办法让实体框架了解更改并对其采取措施?

When this method is called, does Entity Framework take note? That is, is it aware that the stored procedure is called? If so, what does it do to ensure the data it has cached is still in sync with the state of the database after the stored procedure is complete? Is it even able to keep track of what changes the stored procedure is making to the database?

If not, is there a way to make Entity Framework aware of the changes and to do something about it?

ORM是远离底层数据库技术的抽象。 ORM不是数据库引擎,当你使用ADO.NET是数据库引擎时使用不涉及EF的ADO.NET并使用ADO.NET。使用SQL命令对象和
存储过程。所以,不,我不认为ORM正在跟踪数据库引擎正在做什么,也不应该跟踪,因为这不是ORM的工作。 

The ORM is an abstraction away from the underlying database technology. The ORM is not the database engine no more than ADO.NET is the database engine when you  use ADO.NET without EF being involved and using ADO.NET . with SQL Command Objects and the stored procedure. So, no I don't think the ORM is keeping track of what the database engine is doing nor should it have to keep track, because that's not the ORM's job. 

仅仅因为一个是通过 执行存储过程ORM会将存储过程在ORM上的负担放在一边。

Just because one is executing a stored procedure via  the ORM does that put the burden of what the stored procedure is doing on the ORM.

任何ORM的工作....

The job of any ORM....

https://en.wikipedia.org/wiki/Object-relational_mapping

https://en.wikipedia.org/wiki/Object-relational_mapping

<复制>

<copied>

对象关系映射 ORM
O / RM O / R映射工具
计算机科学
编程 在不兼容的
之间转换数据的技术
类型系统 使用
面向对象的 编程语言。这实际上创建了一个"虚拟
对象数据库 "可以在编程语言中使用。

Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language.

< end>

<end>

 


这篇关于实体框架与存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆