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 in Get-Migration in EF Core 8 #32732

Closed
bgoodearl opened this issue Jan 5, 2024 · 8 comments · Fixed by #32920
Closed

NullReferenceException in Get-Migration in EF Core 8 #32732

bgoodearl opened this issue Jan 5, 2024 · 8 comments · Fixed by #32920
Assignees
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@bgoodearl
Copy link

File a bug

See details in issue 32487
efcore/issues/32487

We've spent a fair bit of time trying to reproduce the issue with a cut-down sample, but have not been successful.

A colleague has debugged into the EF code with our client project and found where the exception is being thrown.

I created a test sample with enough entities to have the entity where the exception was thrown, but that sample does not fail.

We can share the new test sample with you and include information about where the client code is failing and the new sample is not failing, but do not want to make the new sample public.

Code that's failing:

static DeleteBehavior DefaultDeleteBehavior(IConventionSkipNavigation conventionSkipNavigation)
{
	if (!conventionSkipNavigation.ForeignKey.IsRequired)
	{
		return DeleteBehavior.ClientSetNull;
	}
	return DeleteBehavior.Cascade;
}

this throws the exception: conventionSkipNavigation.ForeignKey.IsRequired due to ForeignKey being null

This problem is a show-stopper for converting this client project to .NET 8.

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.8.3

@bgoodearl
Copy link
Author

I have a cut down sample of the client solution that reproduces the problem. I can share the code with you folks as long as it's not made public. How would we do that?

@ajcvickers
Copy link
Member

@bgoodearl You can email it to avickers at this place I work. Let me know when you have done so, since it may go to spam.

@bgoodearl
Copy link
Author

Arthur - just sent you an email without a zip. Will follow up with a zip.

@bgoodearl
Copy link
Author

Sent emails with zips

@bgoodearl
Copy link
Author

Updated NuGet packages to 8.0.1 and VS2022 to 17.8.4 - same results

@MantasG-Visma
Copy link

MantasG-Visma commented Jan 18, 2024

we're also encountering the issue when switched to .NET 8 with EF8 and executing "dotnet.exe ef migrations script -i" which worked fine on .NET 7.

Stacktrace:

System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindCollectionMapping(RelationalTypeMappingInfo info, Type modelType, Type providerType, CoreTypeMapping elementMapping)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.<>c.b__8_0(ValueTuple4 k, RelationalTypeMappingSource self) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd[TArg](TKey key, Func3 valueFactory, TArg factoryArgument) at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMappingWithConversion(RelationalTypeMappingInfo mappingInfo, Type providerClrType, ValueConverter customConverter) at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMappingWithConversion(RelationalTypeMappingInfo mappingInfo, IReadOnlyList1 principals)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMapping(Type type, String storeTypeName, Boolean keyOrIndex, Nullable1 unicode, Nullable1 size, Nullable1 rowVersion, Nullable1 fixedLength, Nullable1 precision, Nullable1 scale)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.DefaultValue(Object defaultValue, String defaultValueSql, String columnType, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, ColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__82_0(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList1 operations, IModel model, MigrationsSqlGenerationOptions options) at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList1 operations, IModel model, MigrationsSqlGenerationOptions options)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigrationImpl(String fromMigration, String toMigration, Boolean idempotent, Boolean noTransactions, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 25, 2024
@ajcvickers
Copy link
Member

@MantasG-Visma That looks like a different issue. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers added a commit that referenced this issue Jan 26, 2024
* Don't assume that FK of skip navigation is non-null

Fixes #32732

* Add additional assesion.
@ajcvickers ajcvickers reopened this Jan 26, 2024
ajcvickers added a commit that referenced this issue Jan 26, 2024
* Don't assume that FK of skip navigation is non-null

Fixes #32732

* Add additional assesion.
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.3 Jan 29, 2024
@MantasG-Visma
Copy link

@ajcvickers I have debugged the issue, it seems it is caused by default value of a guid key:

migrationBuilder.AddColumn<Guid>( name: "MyId", table: "MyTable", nullable: false, defaultValue: "0616c0e2-9505-4fe0-0001-000000000000");

When I delete it, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants