Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRE when adding property named "Count" to a property bag entity type #25485

Closed
AndriySvyryd opened this issue Aug 11, 2021 · 1 comment · Fixed by #25809
Closed

NRE when adding property named "Count" to a property bag entity type #25485

AndriySvyryd opened this issue Aug 11, 2021 · 1 comment · Fixed by #25809
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@AndriySvyryd
Copy link
Member

Originally filed at PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1491

This seems to be a bug in EF Core that is only triggered for specific entity property names (e.g. when your property is named Count) in migration snapshots.

The underlying issue seems to be, that EF Core is checking the properties of the wrong type. It does enumerated the properties of the Metadata.ClrType property, but it is not set to the actual entity type (since this is a migration snapshot), but to a type of Dictionary<string, object>.
It then hits the Count property of this Dictionary<string, object>, checks its int type against the expected double type of the entity's property, realizes that they do not match, and finally returns null, which will then throws the NRE.

This is the line with the issue:

memberInfo ??= Metadata.ClrType.GetMembersInHierarchy(propertyName).FirstOrDefault();

memberInfo ??= Metadata.ClrType.GetMembersInHierarchy(propertyName).FirstOrDefault();

@AndriySvyryd, @ajcvickers Reproduced with the following code:

Program.cs
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace IssueConsoleTemplate
{
    public class RegistryOrderServices
    {
        public Guid Id { get; set; }
        public double Count { get; set; }
    }

    public class Context : DbContext
    {
        public DbSet<RegistryOrderServices> RegistryOrderServices { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                var connectionString = "server=127.0.0.1;port=3308;user=root;password=;database=Issue1491";
                var serverVersion = ServerVersion.AutoDetect(connectionString);

                optionsBuilder.UseMySql(connectionString, serverVersion)
                    .UseLoggerFactory(
                        LoggerFactory.Create(
                            b => b
                                .AddConsole()
                                .AddFilter(level => level >= LogLevel.Information)))
                    .EnableSensitiveDataLogging()
                    .EnableDetailedErrors();
            }
        }
    }

    internal static class Program
    {
        private static void Main()
        {
        }
    }
}

Add an initial migration and then another one (can be empty).
Should be reproducible with any provider.

@war-beast
Copy link

The same thing happen with a property named "Values"

@AndriySvyryd AndriySvyryd removed their assignment Sep 1, 2021
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 2, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants