简体   繁体   English

如何使用Entity Framework加载存储过程的结果?

[英]How can I use Entity Framework to load the results of a stored procedure?

I have a challenge. 我有一个挑战。 We have a database that was originally designed to be used with VB6 and most of the functionality sits in stored procedures. 我们有一个最初设计用于VB6的数据库,大多数功能都存储在存储过程中。 I cannot make changes to the stored procedures as the old application will still need to work for a while and I do not have my own copy of the database that I can modify even briefly. 我无法对存储过程进行更改,因为旧的应用程序仍然需要工作一段时间,而且我没有自己的数据库副本,我甚至可以简单地修改它。

So is it possible to execute a stored procedure from EF and have it do it's best to write the results into an array/collection of POCOs? 那么是否可以从EF执行存储过程并让它最好将结果写入POCO的数组/集合中?

I've tried the database first approach and import but EF says the stored procedure does not return any columns and so cannot create a complex type. 我已经尝试了数据库第一种方法和导入,但EF说存储过程不返回任何列,因此无法创建复杂类型。 I've found there are ways to change the stored procedure to allow this to work but I cannot alter the database I'm using. 我发现有一些方法可以改变存储过程以使其工作,但我无法改变我正在使用的数据库。

Another challenge is that the names of the columns in the results are things like 'Date last changed' in other words with spaces. 另一个挑战是结果中列的名称是“日期最后更改”,换句话说是空格。 How will EF try to map these? EF将如何映射这些? Would it become DataLastChanged or possibly Data_last_changed? 它会变成DataLastChanged还是Data_last_changed? Is there a way to mark my POCO with attributes to say how they are mapped? 有没有办法用我的POCO标记属性来说明它们是如何映射的?

What I was hoping for is something like 我希望的是类似的东西

var resuls = efContext.ExecuteStoredProcedure<MyPOCOType>("spName",param1, param2, ...);

And have EF do it's best to match the results to the type. 并且让EF最好将结果与类型相匹配。 Does such a thing exist? 这样的事情存在吗? Incidentally we are using EF4 but I believe 5 is available to us. 顺便提一下,我们使用的是EF4,但我相信我们可以使用EF4。

I think I've cracked part of the problem for myself. 我想我已经为自己解决了部分问题。 The following snippet does what I need. 以下代码片段可以满足我的需求。

using (DbContext context = new DbContext("DBConnectionStringNameFromAppConfig"))
            {

                SqlParameter[] parameters =
                      {
                                        new SqlParameter("@OwnerID", DBNull.Value),
                                        new SqlParameter("@ExternalColorID", colorOwner.ExternalColorID),
                                        new SqlParameter("@ProductionSiteID", DBNull.Value),
                                        new SqlParameter("@PanelstatusNr", DBNull.Value),
                                        new SqlParameter("@DateLastChecked", DBNull.Value),
                                        new SqlParameter("@rowcount", DBNull.Value),
                      };
                var colors = context.Database.SqlQuery<Models.ColorSelectEvaluation>("[dbo].[sp_Color_Select_Evaluation] @OwnerID, @ExternalColorID, @ProductionSiteID, @PanelstatusNr, @DateLastChecked, @rowcount", parameters).ToList();

            }

The confusing this is still the naming of the columns. 令人困惑的仍然是列的命名。 They mostly seem to work but EF is not mapping the resulting column 'Needs evaluation' to the property NeedsEvaluation on my object. 它们似乎主要起作用,但EF并未将结果列“需要评估”映射到我的对象上的属性NeedsEvaluation。

Regarding the column names not matching. 关于列名不匹配。 Another Q&A on stackoverflow deals with this nicely. stackoverflow上的另一个问答很好地解决了这个问题。 Why is my DbModelBuilder configuration ignored when mapping Entity from DbSet<T>.SqlQuery? 为什么在从DbSet <T> .SqlQuery映射实体时忽略了我的DbModelBuilder配置?

To summarise, MS think it would be great but they do not support mapping of names in this way. 总而言之,MS认为它会很棒,但它们不支持以这种方式映射名称。 The only solution is to change the stored procedure and that is no option for me as it would break the legacy applications still using it. 唯一的解决方案是更改存储过程,这对我来说是没有选择的,因为它会破坏仍在使用它的遗留应用程序。

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

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