Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want run stored procedure in sql server by EF for example
public IList<Charts> GetDataChart(int id)
        {
            var cmd = "[dbo].[getdatechart] @id";
            var sendId = new SqlParameter("@id", id);
            var result = DataContext.Database.SqlQuery<Charts>(cmd, sendId).ToList();

            return result;


        }

There is error at run time.because Charts is not table and not defined dbcontext.it is only one class that contain proertis.

What I have tried:

how can i create temporary table by Entity framework code first
Posted
Updated 6-Dec-19 23:49pm

Read Entity Framework 6 - Dynamically creating temporary tables from IQueryable (Part 1 of 2)[^]

It's not simple, and requires a lot of extra work.

You might be better off using a stored procedure to do this and modifying your EF code to use the stored procedure instead.
 
Share this answer
 
As I do not use Entity Framework this may be completely wrong- and this answer is based on my experiences with ADO which shares its underpinnings with EF.

What the problem may be is that your database context is assuming that your command is "text", which is the default type and expects the command to be a "SELECT" statement. If you change the definition of your command and define it as a Stored Procedure you may have some success. You may have some success with something akin to this
C#
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "[dbo].[getdatechart]";
cmd.CommandType = CommandType.StoredProcedure;
 
Share this answer
 
Here is a bit delayed answer.

NuGet Gallery | EF6TempTableKit 1.0.0[^]

There is a code (source & unit tests) on GitHub with a detailed explanation of how to integrate and run EF6TempTableKit extension.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900