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

NullReferenceException when trying to seed a keyless entity #23030

Closed
roji opened this issue Oct 17, 2020 · 3 comments · Fixed by #23562
Closed

NullReferenceException when trying to seed a keyless entity #23030

roji opened this issue Oct 17, 2020 · 3 comments · Fixed by #23562
Labels
area-migrations-seeding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution type-bug
Milestone

Comments

@roji
Copy link
Member

roji commented Oct 17, 2020

Repro:

await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer(...);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasNoKey()
            .HasData(new Blog { Name = "foo" });
    }
}

public class Blog
{
    public string Name { get; set; }
}

Happens on both the latest 5.0 and 3.1.

@smitpatel
Copy link
Contributor

Keyless entities don't have keys so they cannot be seeded using state manager based seeding implementation.
Should throw a better validation or need different implementation for seeding.
#22063

@umitkavala
Copy link
Contributor

I'll have a look at this while I stuck at #23226 :)

@umitkavala
Copy link
Contributor

umitkavala commented Dec 2, 2020

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
    public abstract class SeedingTestBase
    {
        [ConditionalTheory]
        [InlineData(false)]
        [InlineData(true)]
        public virtual async Task Seeding_does_not_leave_context_contaminated(bool async)
        {
            using var context = CreateContextWithEmptyDatabase(async ? "1A" : "1S");
            var _ = async
                ? await context.Database.EnsureCreatedResilientlyAsync()
                : context.Database.EnsureCreatedResiliently();

            Assert.Empty(context.ChangeTracker.Entries());

            var seeds = context.Set<Seed>().OrderBy(e => e.Species).ToList();
            Assert.Equal(2, seeds.Count);
            Assert.Equal("Apple", seeds[0].Species);
            Assert.Equal("Orange", seeds[1].Species);
        }

        protected abstract SeedingContext CreateContextWithEmptyDatabase(string testId);

        protected abstract class SeedingContext : DbContext
        {
            public string TestId { get; }

            protected SeedingContext(string testId)
                => TestId = testId;

            protected override void OnModelCreating(ModelBuilder modelBuilder)
                => modelBuilder.Entity<Seed>()
                    .HasNoKey()
                    .HasData(
                    new Seed {  Species = "Apple" },
                    new Seed {  Species = "Orange" }
                );
        }

        protected class Seed
        {
            //[DatabaseGenerated(DatabaseGeneratedOption.None)]
            //public int Id { get; set; }

            public string Species { get; set; }
        }
    }
}

reproduced with test case. I'll continue from there
image

Its failing before Seeding when decide IsKeyUnknown:

var keyProperties = ((EntityType)EntityType).FindPrimaryKey().Properties // its null here

umitkavala added a commit to umitkavala/efcore that referenced this issue Dec 2, 2020
@AndriySvyryd AndriySvyryd removed their assignment Jan 11, 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 Jan 11, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview1 Jan 27, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-preview1, 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-migrations-seeding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants