实体框架使用UDT调用存储过程 [英] entity framework calling store procedures with UDT

查看:125
本文介绍了实体框架使用UDT调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,在SQL中,使用输入创建一个存储过程

  ALTER PROCEDURE [dbo]。[AddNotice_OC] 
@OperatorName VARCHAR(32)= NULL,
@ModuleCode VARCHAR(10)= NULL,
@NoticeInfo udt_NoticeInfo READONLY,
@Msg NVARCHAR(MAX),
@NoticeId_oc BIGINT,
@ReturnCode INT OUTPUT
AS注意,udt_NoticeInfo是用户定义的SQL表



然后,在.NET实体框架中,我有一个现有的.edmx,我点击模型浏览器,并从数据库执行更新模型。



可以在.edmx文件中找到并添加存储过程,但是缺少udt_NoticeInfo的参数,该过程只是udt_NoticeInfo。



因此,我想知道如何更正.edmx,以便udt_noticeInfo可以包含在模型中?



谢谢。

解决方案

无法直接使用实体框架传递表值参数



检查这个链接和此链接和此< a href =https://stackoverflow.com/questions/8157345/entity-framework-stored-procedure-table-value-parameter>问题,了解如何执行存储过程接受EF中的表值参数。 / p>

它基本上涉及到s


  1. 创建一个 DataTable 保存行

  2. 将datatable作为 SqlParameter 与DbType添加为 SqlDbType.Structured


$ b,使用 ObjectContext.ExecuteStoreCommand()
执行存储过程$ b

I have a problem when trying to create store procedures and calling it by .NET entity framework.

Firstly, in the SQL, a store procedure is created with inputs

ALTER PROCEDURE [dbo].[AddNotice_OC]
    @OperatorName VARCHAR(32) = NULL,
    @ModuleCode VARCHAR(10) = NULL,
    @NoticeInfo udt_NoticeInfo READONLY,
    @Msg NVARCHAR(MAX),
    @NoticeId_oc BIGINT,
    @ReturnCode INT OUTPUT
AS

note that udt_NoticeInfo is user defined table of SQL

Then, in the .NET entity framework, i have an existing .edmx, i click in the Model Browser and perform Update Model from Database.

The store procedure can be found and added in the .edmx file, however the parameter of the udt_NoticeInfo is missing, the process simply the udt_NoticeInfo.

Thus, I want to know how can I correct the .edmx so that the udt_noticeInfo can be included in the model?

Thanks.

解决方案

You cannot pass table valued parameters directly using Entity Framework.

Check this link and this link and this question for how to execute stored procs accepting Table Valued parameter in EF.

It basically involves

  1. creating a DataTable to hold the rows
  2. adding datatable as SqlParameter with DbType as SqlDbType.Structured
  3. executing the stored proc using ObjectContext.ExecuteStoreCommand() by passing the sql command and the parameters

这篇关于实体框架使用UDT调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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