如何首先按实体框架代码创建临时表 [英] How can I create temporary table by entity framework code first

查看:60
本文介绍了如何首先按实体框架代码创建临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过EF在sql server中运行存储过程,例如

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;


        }



运行时出错。因为图表不是表而且未定义dbcontext.it只有一个类包含proertis。



我尝试过:




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

推荐答案

读取实体框架6 - 从IQueryable动态创建临时表(第1部分,共2部分) [ ^ ]



这不简单,需要大量的额外工作。



你可能最好使用存储过程来完成这项工作并修改您的EF代码改为使用存储过程。
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.


由于我不使用实体框架,这可能是完全错误的 - 这个答案是基于我与ADO的经验,它与EF共享其基础。



问题可能是您的数据库上下文假设您的命令是 text,这是默认类型,并期望该命令是SELECT语句。如果更改命令的定义并将其定义为存储过程,则可能会取得一些成功。你可能会有类似的事情取得一些成功

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
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "[dbo].[getdatechart]";
cmd.CommandType = CommandType.StoredProcedure;


这篇关于如何首先按实体框架代码创建临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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