实体框架数据库第一种方法,将字符串作为存储过程的返回类型 [英] Entity framework database first approach having string as return type for stored procedure

查看:56
本文介绍了实体框架数据库第一种方法,将字符串作为存储过程的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用数据库第一实体框架方法处理MVC产品,我在EDM中添加了一些存储过程,但有些是返回字符串而不是模型类型。我删除了model.edmx,从web.config文件中删除了连接字符串,并将model1.edmx文件重新添加到项目中,但是,有些仍然有字符串作为返回类型,我还创建了一个viewmodel类并使用它代替字符串,但没有运气。我希望能够添加过程来返回模型的类型。



Model1.Context类中的存储过程:



I working on an MVC product using Database first entity framework approach, I added a few stored procedure to the EDM, but some are returning string instead of the model type. I have deleted the model.edmx, removed the connection string from the web.config file and re- added model1.edmx file to the project, yet, some still have string as return type, I have also created a viewmodel class and use it in place of the string, yet no luck. I want to be able to add procedure to return type of the model.

Stored Procedure in Model1.Context class:

public virtual ObjectResult<string> PreLoadWorkflowType()
{            
    return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("PreLoadWorkflowType");
}





我希望生成的存储过程看起来像这样:





I want the generated stored procedure to look like this instead:

public virtual ObjectResult<PreLoadWorkflowType> PreLoadWorkflowType()
{               return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PreLoadWorkflowType>("PreLoadWorkflowType");
} 







存储过程:






Stored Procedure:

USE [databsename]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[PreLoadWorkflowType]
AS
BEGIN
SET NOCOUNT ON;
select distinct workflow_type  AS WorkflowType from V_ui_View  where 
workflow_type is not null 
END





我一直在向EDM文件添加存储过程,我之前没有遇到过这种情况,我将不胜感激任何有经验并解决了这个问题的人。



我尝试过的事情:





I have been adding stored procedure to the EDM file all the while, i have not experience this before, I will appreciate assistance from anyone who had experienced and resolved this.

What I have tried:

public virtual ObjectResult<PreLoadWorkflowTypeViewModel> PreLoadWorkflowType()
{               return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PreLoadWorkflowTypeViewModel>("PreLoadWorkflowType");
}

推荐答案

在模型浏览器中选择您的SP,然后在属性窗口中单击返回类型,然后单击省略号按钮打开编辑功能导入对话框。



如果您希望SP返回现有实体的实例,请在返回集合中选择实体和相关的。如果数据与现有实体不匹配,则选择复杂,单击获取列信息,然后单击创建新复杂类型,它将为您创建一个具有相关属性的类,并修改代码,以便它现在返回ObjectResult< YourComplexType>
In the model browser select your SP and in the properties window click "Return Type" then click the ellipses button to open the "Edit Function Import" dialog.

In the "Returns a collection of" if you want the SP to return instances of an existing entity then select Entities and the relevant one. If the data doesn't match an existing entity then select "Complex", click "Get Column Information", then click "Create New Complex Type" and it'll create a class for you with the relevant properties and amend the code so it now returns ObjectResult<YourComplexType>


这篇关于实体框架数据库第一种方法,将字符串作为存储过程的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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