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 #23562

Merged
merged 24 commits into from
Jan 11, 2021

Conversation

umitkavala
Copy link
Contributor

Fixes #23030
I'm not sure about tests. Is it enough or correct place? I've changed base class so implemented classes also should change.
Maybe we can create new test class?

@umitkavala
Copy link
Contributor Author

SqlServer & SqlLite tests passed but InMemory couldn't because its depend on CreateFactory has key.
I assume this fix should apply for all DB types right?

private Func<IInMemoryTable> CreateTable([NotNull] IEntityType entityType, IInMemoryTable baseTable)
=> (Func<IInMemoryTable>)typeof(InMemoryTableFactory).GetTypeInfo()
	.GetDeclaredMethod(nameof(CreateFactory))
	.MakeGenericMethod(entityType.FindPrimaryKey().GetKeyType())
	.Invoke(null, new object[] { entityType, baseTable, _sensitiveLoggingEnabled, _nullabilityCheckEnabled });

[UsedImplicitly]
private static Func<IInMemoryTable> CreateFactory<TKey>(
IEntityType entityType,
IInMemoryTable baseTable,
bool sensitiveLoggingEnabled,
bool nullabilityCheckEnabled)
=> () => new InMemoryTable<TKey>(entityType, baseTable, sensitiveLoggingEnabled, nullabilityCheckEnabled);

@ajcvickers
Copy link
Member

@umitkavala This does not look like the correct fix. The comment from Smit in #23030 says:

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.

This means the correct resolution for this bug is to throw an InvalidOperationException with a message indicating that seeding keyless entity types are not supported.

@umitkavala
Copy link
Contributor Author

@ajcvickers Sure. I'll implement that way. I couldn't make sure that's why I've created as draft. And this rule apply for all right? SqlServer, InMemory, SqlLite. So in that case test I can add test case to the SeedingTestBase.cs

@ajcvickers
Copy link
Member

@umitkavala Sounds good.

@umitkavala
Copy link
Contributor Author

I did required changes.

@umitkavala
Copy link
Contributor Author

Can I convert this draft to PR for final review?

@smitpatel smitpatel marked this pull request as ready for review December 9, 2020 16:15
umitkavala and others added 10 commits December 9, 2020 21:47
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
Co-authored-by: Smit Patel <smitpatel@users.noreply.github.com>
make sure EnsureClean execute first.
@umitkavala
Copy link
Contributor Author

Hi @AndriySvyryd, I think this PR is ready.

protected abstract SeedingContext CreateContextWithEmptyDatabase(string testId);

protected abstract KeylessSeedingContext CreateKeylessContextWithEmptyDatabase(string testId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected abstract KeylessSeedingContext CreateKeylessContextWithEmptyDatabase(string testId);
protected virtual KeylessSeedingContext CreateKeylessContextWithEmptyDatabase()
=> new KeylessSeedingContext(TestStore.AddProviderOptions(new DbContextOptionsBuilder()).Options)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeylessSeedingContext won't be abstract anymore and we won't need KeylessSeedingContext implementations on other classes (KeylessSeedingSqlServerContext, KeylessSeedingSqliteContext, KeylessSeedingInMemoryContext)

public class KeylessSeedingContext : DbContext
        {
            public KeylessSeedingContext(DbContextOptions options)
                : base(options)
            {
            }

Other implementations removed :
KeylessSeedingSqlServerContext, KeylessSeedingSqliteContext, KeylessSeedingInMemoryContext
@AndriySvyryd AndriySvyryd merged commit f97cf05 into dotnet:main Jan 11, 2021
@AndriySvyryd
Copy link
Member

Thanks for your contribution

@umitkavala
Copy link
Contributor Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NullReferenceException when trying to seed a keyless entity
4 participants