简体   繁体   中英

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. 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?

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. 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? Would it become DataLastChanged or possibly Data_last_changed? Is there a way to mark my POCO with attributes to say how they are mapped?

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. Does such a thing exist? Incidentally we are using EF4 but I believe 5 is available to us.

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.

Regarding the column names not matching. Another Q&A on stackoverflow deals with this nicely. Why is my DbModelBuilder configuration ignored when mapping Entity from DbSet<T>.SqlQuery?

To summarise, MS think it would be great but they do not support mapping of names in this way. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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