简体   繁体   English

实体框架中的OPENQUERY存储过程

[英]OPENQUERY stored procedure in Entity framework

I am trying to create a stored procedure that is getting values from a linked to our Sql Server DB2 server.In the stored procedure I have this query: 我正在尝试创建一个存储过程,该存储过程从链接到Sql Server DB2服务器的值中获取值。在存储过程中,我有以下查询:

DECLARE @CarID nvarchar(10)
DECLARE @TSQL varchar(8000)
SET @CarID = '1111'
  SELECT  @TSQL = 'SELECT * FROM OPENQUERY(LINKEDSERVER,''SELECT * FROM TestTable WHERE Column LIKE ''''' + @CarID + '%' + ''''''')'
  EXEC (@TSQL)

Everithing is working fine but when I add the stored procedure to the Entity model the signature of the procedure is: 一切都很好,但是当我将存储过程添加到实体模型时,该过程的签名为:

GetUsers(string):int

But when I run the procedure returns data rows. 但是,当我运行该过程时,将返回数据行。 How can I modify the procedure to return a data set not an integer? 如何修改过程以返回不是整数的数据集?

EF handles a stored procedure somewhat similar as a scalar function. EF处理存储过程与标量函数有些相似。 EF doesn't know how many datasets and which columns will be selected in your stored procedure, therefore cannot generate the classes. EF不知道您的存储过程中将选择多少个数据集和哪些列,因此无法生成类。

Best way to select something on a linked server is with a view. 在链接服务器上选择某项的最佳方法是使用视图。 Simply create the view with the four part name and add it to your EF datamodel. 只需使用四个部分的名称创建视图,然后将其添加到EF数据模型即可。 Then EF will be able the generate the class. 然后,EF将能够生成该类。

CREATE VIEW [dbo].[vTestTable]
AS
    Select * from [LINKEDSERVER].[DatabaseName].[Schema].[TestTable]
GO

Then in .NET 然后在.NET中

var result = db.vTestTable.Where(t=> t.Column.StartsWith(CarId)).ToList();

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

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