简体   繁体   中英

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:

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 doesn't know how many datasets and which columns will be selected in your stored procedure, therefore cannot generate the classes.

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. Then EF will be able the generate the class.

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

Then in .NET

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

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