EF Core, вызывающий хранимую процедуру с использованием FromSql, выдает исключение InvalidOperationExceptionC#

Место общения программистов C#
Ответить
Гость
 EF Core, вызывающий хранимую процедуру с использованием FromSql, выдает исключение InvalidOperationException

Сообщение Гость »


I am using EF Core 7 and trying to call a stored procedure with the following code, but the EF Core query generator throws an exception. I'm using the syntax from the MS Docs and I've checked these other examples and I just don't see why my code is failing

My code:

SqlParameter casinoState = new SqlParameter("CasinoState", casinoStateFilter); SqlParameter isActive = new SqlParameter("IsActive", isActiveFilter); SqlParameter prohibitedCountry = new SqlParameter("ProhibitedCountry", prohibitedCountryFilter); SqlParameter softwareProviderId = new SqlParameter("SoftwareProviderId", softwareProviderFilter); SqlParameter casinoName = new SqlParameter("CasinoName", casinoNameFilter); FormattableString sql = $"exec CasinoIdSerach @CasinoState={casinoStateFilter}, @IsActive={isActive}, @ProhibitedCountry={prohibitedCountryFilter}, @SoftwareProviderId={softwareProviderFilter}, @CasinoName={casinoNameFilter}"; var casinos = _context.Casinos.FromSql(sql).ToList(); I also tried _context.Casinos.FromSql(sql).ToList(); with the same result

The exception

System.InvalidOperationException: ''FromSql' or 'SqlQuery' was called with non-composable SQL and with a query composing over it. Consider calling 'AsEnumerable' after the method to perform the composition on the client side.'

This is the example code from MS and I don't see why this would work, but mine does not

var user = new SqlParameter("user", "johndoe"); var blogs = context.Blogs .FromSql($"EXECUTE dbo.GetMostPopularBlogsForUser @filterByUser={user}") .ToList(); I've tried a few variations of the SQL string e.g.
  • FormattableString sql = $"exec CasinoSearch @CasinoName='%test%'";
  • FormattableString sql = $"select * from Casino";

Now, the second one executes without an exception, but obviously the query is not what I want

I have successfully used the following with a stored procedure, but this example does not need to return a data set

int result = _context.Database.ExecuteSqlRaw(query, parameters.ToArray());


Источник: https://stackoverflow.com/questions/780 ... nexception
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»