Include with FromSqlRaw and stored procedure in EF Core 3.1

Shortly, you can’t do that (at least for SqlServer). The explanation is contained in EF Core documentation – Raw SQL Queries – Composing with LINQ: Composing with LINQ requires your raw SQL query to be composable since EF Core will treat the supplied SQL as a subquery. SQL queries that can be composed on begin … Read more

How can I stop EF Core from creating a filtered index on a nullable column

Creating filtered index excluding NULL values is the default EF Core behavior for unique indexes containing nullable columns. You can use HasFilter fluent API to change the filter condition or turn it off by passing null as sql argument: entityBuilder.HasIndex(e => new { e.LevelId, e.Name, e.DeletedAt }) .IsUnique() .HasFilter(null);

Reconfigure dependencies when Integration testing ASP.NET Core Web API and EF Core

@ilya-chumakov’s answer is awesome. I just would like to add one more option 3. Use ConfigureTestServices method from WebHostBuilderExtensions. The method ConfigureTestServices is available in the Microsoft.AspNetCore.TestHost version 2.1(on 20.05.2018 it is RC1-final). And it lets us override existing registrations with mocks. The code: _server = new TestServer(new WebHostBuilder() .UseStartup<Startup>() .ConfigureTestServices(services => { services.AddTransient<IFooService, MockService>(); … Read more

Return multiple values from a C# asynchronous method

After playing with the code I was able to figure it out. Here is the solution. public async Task DeleteSchoolTask(int schoolNumber, int taskDetailId) { var result = await GetTaskTypeAndId(taskDetailId); int taskId = result.Item1; string taskType = result.Item2; // step 1: delete attachment physically from server var fileService = new FileService(Logger, CurrentUser); var relativeFilePath = $”{schoolNumber}\\{Consts.RM_SCHOOL}\\{taskDetailId}”; … Read more

Scaffold-DbContext – CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. par’name’ en-us is an invalid

You have to make changes in .csproj file of your project, need to add InvariantGlobalization attribute to false in PropertyGroup. This is how it looks. <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <InvariantGlobalization>false</InvariantGlobalization> </PropertyGroup>

Entity Framework Core migration – connection string

All the examples I’ve seen involve either hard-coding the connection string or putting it in my ASP.NET Core application’s settings files. If you aren’t using ASP.NET Core, or maybe, I don’t know, don’t want to have your local environment’s database details committed to source control, you can try using a temporary environment variable. First, implement … Read more

BeginTransaction with IsolationLevel in EF Core

The EF Core code is exactly the same. DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); The only difference is that in EF Core the method with isolation level (as many others) is an extension method, defined in RelationalDatabaseFacadeExtensions class, and importantly, located in Microsoft.EntityFrameworkCore.Relational assembly. So if you have using Microsoft.EntityFrameworkCore; and don’t see it, add reference to the Microsoft.EntityFrameworkCore.Relational.dll assembly … Read more