dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.59k stars 1.95k forks source link

Explicitly mention that raw SQL isn't supported with InMemory #2400

Closed CriggerMarg closed 4 years ago

CriggerMarg commented 4 years ago

It seems that EF Core 3.1 does not contain raw sql methods anymore


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

ajcvickers commented 4 years ago

@CriggerMarg As it says at the end of the page, the 2.2 methods were renamed. See also https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#fromsql

CriggerMarg commented 4 years ago

@ajcvickers Thing is there is no such methods at all in Microsoft.EntityFrameworkCore v 3.1.4.

Look at https://github.com/dotnet/efcore/blob/master/src/EFCore/DbSet.cs code

ajcvickers commented 4 years ago

@CriggerMarg It does. From the page:

EF Core version 2.2 and earlier had two overloads of method named FromSql, which behaved in the same way as the newer FromSqlRaw and FromSqlInterpolated. It was easy to accidentally call the raw string method when the intent was to call the interpolated string method, and the other way around. Calling wrong overload accidentally could result in queries not being parameterized when they should have been.

CriggerMarg commented 4 years ago

@ajcvickers there is no any method in DbSet class that starting with "From...".

ajcvickers commented 4 years ago

https://github.com/dotnet/efcore/blob/release/3.1/src/EFCore.Relational/Extensions/RelationalQueryableExtensions.cs

JasonLiebgott commented 4 years ago

public static RelationalDataReader ExecuteSqlQuery(this DatabaseFacade databaseFacade, string sql, params object[] parameters) { var concurrencyDetector = databaseFacade.GetService();

        using (concurrencyDetector.EnterCriticalSection())
        {
            var rawSqlCommand = databaseFacade
                .GetService<IRawSqlCommandBuilder>()
                .Build(sql, parameters);

            return rawSqlCommand
                .RelationalCommand
                .ExecuteReader(
                    databaseFacade.GetService<IRelationalConnection>(),
                    parameterValues: rawSqlCommand.ParameterValues);
        }
    }

This method is no longer working (after update to Core 3.1.4) -- any ideas on where to start with getting it working?

CriggerMarg commented 4 years ago

@ajcvickers thing is I installed ef core 3.1.4 and I don't have these methods at all.

ErikEJ commented 4 years ago

Do you have the required using statement?

CriggerMarg commented 4 years ago

Ok I fugured that out.

To get RelationalQueryableExtensions methods family working in my app I had to install Microsoft.EntityFrameworkCore.Relational package.

It's not listed anywhere in documentation. Please update it, or let me create PR. Let me know which option would work for you.

ErikEJ commented 4 years ago

@CriggerMarg What packages did you have installed?

CriggerMarg commented 4 years ago

@ErikEJ here is my sample project csproj file, whole.

Project Sdk="Microsoft.NET.Sdk">

PropertyGroup> OutputType>Exe TargetFramework>netcoreapp3.1 /PropertyGroup>

ItemGroup> PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" /> PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.4" /> /ItemGroup>

/Project>

ErikEJ commented 4 years ago

You need to mark all the xml as code in markup

CriggerMarg commented 4 years ago

I tried, but gave up. So removed all opened tags

ErikEJ commented 4 years ago

So originally you only had Microsoft.EntityFrameworkCore installed?? That is very unusual.

CriggerMarg commented 4 years ago

How should I know to install other packages? They didn't installed automatically nor from package manager and from popup when I typed DbContext in my code.

ErikEJ commented 4 years ago

From the docs perhaps? https://docs.microsoft.com/da-dk/ef/core/get-started/?tabs=netcore-cli#install-entity-framework-core

CriggerMarg commented 4 years ago

Could you point me where Microsoft.EntityFrameworkCore.Relational is listed in this article please?

ErikEJ commented 4 years ago

Installing a relational provider will due to NuGet magic automatically install Microsoft.EntityFrameworkCore.Relational. https://docs.microsoft.com/en-us/nuget/concepts/package-installation-process

CriggerMarg commented 4 years ago

Does not happenning when you install InMemory adapter though.

I think it's not constructive to mention different part of documentation when I clearly pointed what's wrong with this particular one and suggested how to fix it to avoid confusion.

roji commented 4 years ago

@CriggerMarg InMemory brings in Microsoft.EntityFrameworkCore, but not Microsoft.EntityFrameworkCore.Relational, because InMemory isn't a relational provider. That's why the raw SQL methods aren't available there - InMemory cannot run SQL...

CriggerMarg commented 4 years ago

@roji that makes sense, thank you! Could it still be mentioned in documentation?

roji commented 4 years ago

We already do in various places.

CriggerMarg commented 4 years ago

Still there is a man who got confused so there is room for improvement

roji commented 4 years ago

Submitted #2402 to specifically call out the lack of raw SQL support in InMemory.

CriggerMarg commented 4 years ago

Great, I'll closing this one

roji commented 4 years ago

Reopening, to be closed when the PR is merged.