简体   繁体   English

如何将自定义SQL Server类型转换为Entity Framework生成的代码

[英]How to get custom SQL Server type into Entity Framework generated code

There is a TABLE Type defined in SQL server: 在SQL Server中定义了一个TABLE Type:

CREATE TYPE RealtySearchResult AS TABLE
(
realtyId int not null,
OwnerId int not null,
...)

And stored procedure: 和存储过程:

CREATE PROCEDURE [dbo].[SearchRealty]
(@fulltext nvarchar(200) null,
 @skipRows int,
 @pageCount int,
....
)
AS
BEGIN

DECLARE @SQL nvarchar(max)
DECLARE @result RealtySearchResult

CREATE TABLE #TEMP
(
realtyId int not null,
OwnerId int not null,
...
)

set @SQL = N'
INSERT INTO #TEMP
SELECT 
realty.Id AS realtyId,
realty.OwnerId,
....join with fulltext catalog.... WHERE....@pageCount .....@skipRows'

-- sp_executesql cannot write to local table variable @result,
-- that is why it reads to temp table and then to @result

exec sp_executesql @SQL, N'@skipRows int, @pageCount int', @skipRows, @pageCount

INSERT INTO @result SELECT * FROM #TEMP
SELECT * FROM @result

END

And then in Visual Studio I update the model from database and a new method (wrapper for store procedure SearchRealty) is generated, but it does not contains generated code for returning complex type. 然后在Visual Studio中,我从数据库更新模型,并生成了一个新方法(用于存储过程SearchRealty的包装器),但是它不包含用于返回复杂类型的生成代码。

I would expect that EntityFramework should be able to recognize that the store procedure returns defined table type RealtySearchResult and should generate wrapper for it. 我希望EntityFramework应该能够识别存储过程返回定义的表类型RealtySearchResult并应该为其生成包装器。 I am too lazy to write the complex return type by myself in C# again (I just wrote it in SQL). 我懒得再一次用C#自己编写复杂的返回类型(我只是用SQL编写)。 It is really needed? 真的需要吗?

Can I just generate wrapper for RealtySearchResult type in EntityFramework somehow? 我能以某种方式为EntityFramework中的RealtySearchResult类型生成包装器吗?

I use Visual Studio 2017 and EntityFramework 6. 我使用Visual Studio 2017和EntityFramework 6。

It sounds as duplicate as Stored procedure in Entity Framework database first approach but once I click the button Get Column Information I got message "The selected stored procedure or function returns no columns". 听起来与Entity Framework数据库第一种方法中的存储过程重复,但是单击“获取列信息”按钮后,出现消息“所选存储过程或函数不返回任何列”。

屏幕截图

Analysis 分析

Based on link Entity Framework not getting column info on a different schema provided by kirsten I realize that EntityFramework execute stored procedure with mode 基于链接Entity Framework未获得 kirsten提供的不同架构上的列信息,我意识到EntityFramework使用模式执行存储过程

SET FMTONLY ON FMTONLY开启

It means it strips all condition and dynamic SQL. 这意味着它将剥离所有条件和动态SQL。 This result in empty temporary table and procedure failing during receiving metadata from EntityFramework. 这导致在从EntityFramework接收元数据期间,空的临时表和过程失败。

Solution

To help the designer to get metadata without dynamic SQL. 帮助设计人员在不使用动态SQL的情况下获取元数据。 Count with that conditions are removed. 计数与该条件被删除。 Following code does a job: 以下代码可以完成工作:

DECLARE @result RealtySearchResult

IF 0=1
BEGIN
    SELECT * FROM @result
    RETURN
END

During execution of store procedure by EntityFramework (in order to get metadata), condition 0=1 is removed and empty table of Table type is returned which is enough to get metadata. 在由EntityFramework执行存储过程期间(为了获取元数据),条件0 = 1被删除,并且返回了Table类型的空表,该表足以获取元数据。 This code is never trigerred in production because of impossible condition. 由于不可能的条件,该代码在生产中永远不会被触发。

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

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