Skip to content

Debug view threw Unable to cast object of type 'Microsoft.EntityFrameworkCore.Metadata.RuntimeNavigation' to type 'Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase' #35276

@romanimm

Description

@romanimm

We have a deep integration into ef core. As a safety measure with each update we compare the generated ef-model using the debug string.

With ef core 9 annotations are no longer included (using sqlite). For example the MaxLength attribute outputs the following error:
Debug view threw Unable to cast object of type 'Microsoft.EntityFrameworkCore.Metadata.RuntimeNavigation' to type 'Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase'.. Please report this at https://github.com/dotnet/efcore

using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

using var db = new BloggingContext();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();

Console.WriteLine(
    db.Model.ToDebugString(
        MetadataDebugStringOptions.LongDefault |
        MetadataDebugStringOptions.IncludeAnnotations |
        MetadataDebugStringOptions.IncludePropertyIndexes)
);

public class Blog
{
    public int BlogId { get; set; }

    [MaxLength(300)] public string Url { get; set; }
    public ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; } = null!;
}

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite($"Data Source={@"blog.db"}").EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().HasQueryFilter(b => !b.Url.Contains("hidden"))
            .HasData(
                new Blog
                {
                    BlogId = 1,
                    Url = "http://blogs.msdn.com/adonet",
                },
                new Blog
                {
                    BlogId = 2,
                    Url = "hidden",
                });

        modelBuilder.Entity<Post>().HasData(
            new Post { PostId = 33, Title = "normal post", BlogId = 1 },
            new Post { PostId = 2, Title = "post in hidden blog", BlogId = 2 }
        );
    }
}

Include provider and version information

EF Core version: 9.0.0
Database provider: Sqlite
Target framework: .NET 8.0

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions