简体   繁体   English

从ADO.NET迁移到实体框架

[英]Migrate from ADO.NET to Entity Framework

I have my project set up on ASP.NET MVC 4 using ADO.NET. 我使用ADO.NET在ASP.NET MVC 4上设置了我的项目。

But I think that my Data Access Layer is not upto the mark. 但是我认为我的数据访问层还不够完善。 In my company, the data fetching works this way - First the indexes of all the columns of the query result are stored in variables. 在我的公司中,数据获取以这种方式工作-首先,查询结果的所有列的索引都存储在变量中。 And then, values are fetched using those index variables. 然后,使用这些索引变量获取值。 It saves times because DataReader["Name"] will be slower than DataReader[0] 因为DataReader["Name"]会比DataReader[0]慢,所以可以节省时间

But this approach makes my dev speed slow (mapping variables etc). 但是这种方法使我的开发速度变慢(映射变量等)。 Plus, while fetching a single record, it actually does double the work - First it gets indexes, then it fetches data, instead of just fetching the data. 另外,在获取单个记录的同时,它实际上完成了两倍的工作-首先获取索引,然后获取数据,而不仅仅是获取数据。

I am thinking to migrate to EF. 我正在考虑迁移到EF。

Whatever documentation I have seen about EF, I am interpreting it in a way that, with EF, my already written Stored Procs will be of no use. 无论我看过有关EF的任何文档,我都以某种方式解释它,而使用EF,我已经写好的Stored Procs将毫无用处。

Is it so, if I migrate to EF now, I will have to write code for all my CRUD operations again? 是的,如果我现在迁移到EF,我将不得不再次为所有CRUD操作编写代码吗?

There are different way to call a stored procedure. 调用存储过程有不同的方法。 I personaly prefer to use EDMX files. 我个人更喜欢使用EDMX文件。

There are only few steps: 只有几个步骤:

  • Import Stored Procedure 导入存储过程

在此处输入图片说明

  • Create function Import 创建功能导入

Here is an example where I wrapped a function import into a method: 这是一个将function import包装到方法中的示例:

    private static List<GetEmployees_Result> GetEmployeeList()
    {
        using (var db = new DBEntities())
        {
            //do some crazy Linq
            ObjectResult<GetEmployees_Result> listOfEmployees = db.GetEmployees("USA");
            return listOfEmployees.ToList();
        }
    }

Thats it, now compare that with the tones of ADO.NET code you need to write to do the same thing! 就是这样,现在将您需要编写的代码与ADO.NET代码的音调进行比较!

SAMPLE PROJECT 样品项目

You might be doing it for the wrong reasons. 您可能出于错误原因这样做。

If the performance reason between DataReader["name"] and DataReader[0] makes a noticeable difference in your project, adding another layer of indirection (Entity Framework) will probably make performance worse. 如果DataReader["name"]DataReader[0]之间的性能原因在您的项目中产生了明显的不同,则添加另一层间接寻址(实体框架)可能会使性能变差。

However, I rather assume that this difference is not really relevant in your case, and your company is doing it out of a misdirected desire to micro-optimize. 但是,我宁愿认为这种差异与您的情况并没有真正的关系 ,并且您的公司之所以这样做是出于对微观优化的误导。

My advise is to try EF, try using DataTables ( DataRow.Field provides type-safe access to query results), compare it with your current approach and then choose whatever makes your development life easier. 我的建议是尝试EF,尝试使用DataTables( DataRow.Field提供对查询结果的类型安全访问),将其与当前方法进行比较,然后选择使开发工作更轻松的任何方法。 In most cases, the data access time will largely dominate any "column name lookup" time, so I would not worry too much about that. 在大多数情况下,数据访问时间将在所有“列名查找”时间中占主导地位,因此我不必为此担心太多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM