Migration fails in EF7 when mapping TVF using a model with 'string' properties

See original GitHub issue

After 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:open
  • Created 8 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SLAVONchickcommented, Aug 9, 2023

I found that modifying the code, adding this into the OnModelCreating function, solves the issue:

modelBuilder.Entity<Data>().ToFunction(nameof(TableValueFunctionTest));

The problem is that the ‘Data’ type, in my case, is being used into multiple functions, but that can be solved by calling it for the first function, or creating multiple classes that inherit from ‘Data’ for every case. Nevertheless, this is not explained in the documentation for user defined function mapping (https://learn.microsoft.com/en-us/ef/core/querying/user-defined-function-mapping), nor is detailed as a breaking change in EF7.

Thanks a lot! This seems to work. (Even for functions defined in separate static class mapped to built-in DBMS function with [DbFunction] attribute)

0reactions
AndriySvyrydcommented, May 15, 2023

modelBuilder.Entity<Data>().ToTable((string?)null);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breaking changes in EF Core 6.0
Enum properties can be mapped to string columns in the database using HasConversion<string>() or EnumToStringConverter . This results in EF Core ...
Read more >
Breaking changes in EF Core 7.0 (EF7)
Complete list of breaking changes introduced in Entity Framework Core 7.0 (EF7)
Read more >
Migrating Entity Framework 6 projects to ...
The article provides some possible problems and solutions that may arise when migrating a project with a small part of the Entity Framework ......
Read more >
Entity Framework 7 keeps creating the same migration, and ...
Properties that I've removed from my model are not showing up in the migration. The code that it keeps generating over and over...
Read more >
Migrating from entity framework 6 to Core - YouTube
... Moving to EF Core [21:41] - Mapping many-to-many mapping in EF ... data from navigation properties [29:51] - Defining composite keys in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found