dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.51k stars 3.13k forks source link

Help migrating out of `GetColumnName()` removed in .NET 6 #26565

Closed bruno-garcia closed 1 year ago

bruno-garcia commented 2 years ago

We (nuget-trends) were happily relying on this answer from @ajcvickers to keep the original code even after we bumped to .NET 5:

The method is obsolete because it will not always give the correct answer for newer models. For EF Core 5.0, this only means models that have TPT inheritance mapping, but as we add more features there will be more cases where the method will give the wrong answer.

If you have a model for 3.1 and you haven't changed it to use TPT, then you should be okay calling the implementation above in 5.0.

Now, we're targeting .NET 6 and the obsolete method is gone. It's unclear to me how I can use StoreObjectIdentifier.Create given the information I have in scope.

The code we want to move to .NET 6 is here:

https://github.com/dotnet/nuget-trends/blob/b99c6d484db384049138b40bd9bc9f72fcc4db0d/src/NuGetTrends.Data/BasePostgresContext.cs#L64

Help appreciated as this is the only blocker we have to release NuGet Trends on .NET 6 RC2.

YeskaNova commented 2 years ago

You only need to give it the tablename:

           foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                var tableName = entity.GetEntityTableName();
                foreach (var p in entity.FindPrimaryKey().Properties)
                    p.SetColumnName(p.GetColumnName(StoreObjectIdentifier.Table(tableName)).Replace("Dbo", "", StringComparison.InvariantCultureIgnoreCase));
            }
ajcvickers commented 2 years ago

@bruno-garcia Sorry, I didn't mean to imply by my answer that it was okay to keep calling the obsolete method. I meant it was okay to use the code contained in the implementation of the obsolete method. That is:

var columnName = property.GetColumnName(
    StoreObjectIdentifier.Table(property.DeclaringEntityType.GetTableName(), null));

This is also described in the breaking changes documentation.

bruno-garcia commented 2 years ago

Oh, that helps a lot. Thanks @YeskaNova and @ajcvickers.