Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass table valued parameters to stored procedure in entity framework core #7848

Closed
AVin9 opened this issue Mar 10, 2017 · 13 comments
Closed
Assignees
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@AVin9
Copy link

AVin9 commented Mar 10, 2017

I asked the same question with issue #7820. Divega responded for that issue and that issue was closed. That's why I'm creating the issue again. Thanks, Divega, for the response. But the response was showing the sample of passing table valued parameter to an inline sql. But I have to pass the table valued parameter to an existing stored procedure in entity framework 7. Is it possible to do that?

@divega
Copy link
Contributor

divega commented Mar 10, 2017

But I have to pass the table valued parameter to an existing stored procedure in entity framework 7.

@AVin9 I am not really sure I understand what you mean by that. This is why my answer described how to do it with inline SQL:

  1. Stored procedures are only supported in raw/inline SQL in Entity Framework Core.
  2. Table Valued Parameters are only supported in raw/inline SQL in Entity Framework Core because we just pass any SqlParameter through to the underlying ADO.NET provider.
  3. We initially used "Entity Framework 7" as the name for EF Core, but there is no released product called "Entity Framework 7" so I am not 100% which product you are referring to.

I will leave this issue open so that we can look at what to do with it in triage. I am almost sure we have existing issues covering adding support for stored procedure mapping and TVPs.

Also for future reference, you can just re-open a closed issue if you are the original poster.

@AVin9
Copy link
Author

AVin9 commented Mar 10, 2017

Currently we are using the frameworks Dnx451 and EntityFramework.MicrosoftSqlServer 7.0.0-rc1-final. I use the following syntax for querying the database with stored procedure.

Context.Person.FromSql ("exec insert person @name1='{0}'",name);

@AVin9
Copy link
Author

AVin9 commented Mar 10, 2017

Currently the syntax I use for querying the database is the following.

Context.Person.FromSql ("exec GetPerson @name1='{0}'",name);

@divega
Copy link
Contributor

divega commented Mar 11, 2017

@AVin9 please upgrade to a released version of EF Core, e.g. you can use Microsoft.EntityFrameworkCore.SqlServer version 1.1.1.

The prerelease version you are using does not contain the feature that allows using instances of SqlParameter with FromSql(), which is required to work with TVPs. Issue #3115 represented that feature and was fixed in RC2.

Re the calling syntax in the SQL, I don't remember if {0} works with SqlParameter. The @p syntax I used in the sample at https://gist.github.com/divega/f0f88bf16f35641239cfd9bc534e8d7c does work.

@AVin9
Copy link
Author

AVin9 commented Mar 11, 2017

Currently in my company, we are using DNX 451 and not Core. When I just changed the Microsoft.EntityFrameworkCore.SqlServer version 1.1.1, i'm getting several errors, eg: Services collection in Startup.cs. Where can I get a sample, to see what all changes I have to make in order to upgrade it to Microsoft.EntityFrameworkCore.SqlServer version 1.1.1.

Thank you for your help.

@ajcvickers
Copy link
Member

@divega to follow up with upgrade information.

@divega
Copy link
Contributor

divega commented Mar 13, 2017

@AVin9 there have been several changes since DNX RC1. One of the main breaking changes was the rename of many of our packages, which happened in RC2. Here you will find some guidance on how to upgrade from RC1 to RC2 (most of it should still apply):

https://docs.microsoft.com/en-us/ef/core/miscellaneous/rc1-rc2-upgrade

We have also recently reached RTM for the .NET Core tooling. The main change there is the move from using project.json for the definition of projects to an MSBuild-based csproj files. We encourage everyone doing .NET Core development to migrate to the new tooling and from project.json to csproj files. Assuming you were using Visual Studio 2015 for your DNX development, you should now use Visual Studio 2017 for .NET Core development.

@divega
Copy link
Contributor

divega commented Mar 13, 2017

By the way, if you find any problems migrating from RC1 to RC2 and the RTM and the final version of the tooling, it might be easier to create a new project using the default templates and the copy the code files.

I am closing this issue based on the belief that I have answered your questions. But feel free to reopen if you still have problems using TVPs after upgrading or creating a new issue if you hit any blocking problems unrelated to TVPs.

@divega divega closed this as completed Mar 13, 2017
@divega divega added closed-no-further-action The issue is closed and no further action is planned. and removed type-investigation labels Mar 13, 2017
@smalik03
Copy link

Hello @divega I tried using the TVP example you have given in the gist, but I keep getting the following error-

No mapping to a relational type can be found for the CLR type 'TableValuedParameterBuilder'.

@divega divega modified the milestones: 2.0.0, 2.0.0-preview1 May 8, 2017
@divega divega added type-investigation and removed closed-no-further-action The issue is closed and no further action is planned. labels May 8, 2017
@divega
Copy link
Contributor

divega commented May 8, 2017

Reopening so that I can follow up.

@divega divega reopened this May 8, 2017
@divega divega modified the milestones: Backlog, 2.0.0 Jun 13, 2017
@smitpatel smitpatel changed the title How to pass table valued parameters to stored procedure in entity framework 7 How to pass table valued parameters to stored procedure in entity framework core Oct 31, 2017
@tbirgi
Copy link

tbirgi commented Feb 12, 2019

guys, Anyone has a working sample?
i am also facing somewhat similar issue. My stored proc is getting executed but this table Value is not getting passed. I am using EF core to execute a stored proc and from logs it seems Data Table is getting passed as empty. here is what i am doing.

`var tbl = new DataTable();
tbl.Columns.Add("id", typeof(string));
foreach (var item in stringList)
{ tbl.Rows.Add(item);
}
SqlParameter Parameter = new SqlParameter();
Parameter.ParameterName = "@UDT";
Parameter.SqlDbType = SqlDbType.Structured;
Parameter.Value = tbl;
Parameter.TypeName = "dbo.StringList"; //This is sqlParameter getting passed below.

_dbContext.Set().FromSql(sql,sqlParameter);``

Here is what i have in EF log.
Executing DbCommand [Parameters=[@udt='' (Nullable = false) (DbType = Object)]

@tbirgi
Copy link

tbirgi commented Feb 12, 2019

Looks i figured. It works if we embed @param .
ie sql="EXEC dbo.FindChunks @UDT"

@divega divega added closed-no-further-action The issue is closed and no further action is planned. and removed type-investigation labels Jul 2, 2019
@divega divega removed this from the Backlog milestone Jul 2, 2019
@divega divega closed this as completed Jul 2, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@SolAnurag
Copy link

SolAnurag commented Sep 13, 2023

Did anyone get this? I'm using this way in EntityFrameworkCore.SqlServer 7.0.8

SqlParameter IdsParameter = new()
{
ParameterName = "@ids",
SqlDbType = SqlDbType.Structured,
DbType = DbType.Object,
Value = request.Ids
};

Context.xyz.FromSqlRaw($"exec dbo.xyz @ids = {IdsParameter.Value}");

request.Ids is a IEnumerable tried with List.

I keep getting the same error
Operand type clash: nvarchar is incompatible with Ids

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

6 participants