dotnet / dotnet/efcore

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

Open
#30,142 12 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-model-building customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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(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 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:
```


Exe
net7.0
enable





runtime; build; native; contentfiles; analyzers; buildtransitive
all




all
runtime; build; native; contentfiles; analyzers; buildtransitive

```
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.