Migration fails in EF7 when mapping TVF using a model with 'string' properties
See original GitHub issueAfter updating to EF7, when trying to add a migration I get an error with the following text:
An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
System.InvalidOperationException: Cannot scaffold C# literals of type 'System.Reflection.NullabilityInfoContext'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
After a lot of testing I found that the problem was mapping table valued functions from the DB, but only if the mapped model contains ‘string’ properties. Otherwise, it works normally.
Steps to reproduce
Here is a small code to reproduce the issue in a console app:
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Test;
public class Program
{
static void Main(string[] args)
{
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("ConnectionString")))
.Build();
host.Run();
}
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDbFunction(typeof(ApplicationDbContext).GetMethod(nameof(TableValueFunctionTest), new[] { typeof(DateTime), typeof(decimal) }));
}
public IQueryable<Data> TableValueFunctionTest(DateTime param1, decimal param2) => FromExpression(() => TableValueFunctionTest(param1, param2));
}
[Keyless]
public class Data
{
public string Text { get; set; }
public int Number { get; set; }
public long BigNumber { get; set; }
}
The .csproj file contains this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
In the previous code, commenting out the property “Text”, or changing its type (I tested with int, decimal, double, and DateTime) makes the migration work. Also downgrading to EF 6.0.10 works.
By the way, I tried adding the migration using the dotnet-ef tool (updated to 7.0.2) with the command:
dotnet ef migrations add Test
Also tried with the old
Add-Migration Test
Further technical details
EF Core version: 7.0.2 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 7.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.4
Issue Analytics
- State:
- Created 8 months ago
- Comments:6 (3 by maintainers)
Top Related StackOverflow Question
Thanks a lot! This seems to work. (Even for functions defined in separate static class mapped to built-in DBMS function with [DbFunction] attribute)
modelBuilder.Entity<Data>().ToTable((string?)null);