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

How to upgrade use of GetColumnName() method #4024

Closed
ygoe opened this issue Sep 5, 2022 · 3 comments · Fixed by #4050
Closed

How to upgrade use of GetColumnName() method #4024

ygoe opened this issue Sep 5, 2022 · 3 comments · Fixed by #4050

Comments

@ygoe
Copy link

ygoe commented Sep 5, 2022

I'm upgrading an application from ASP.NET Core 3.1 to 6.0. Reading the migration documents, I found this:

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/breaking-changes#getcolumnname-obsolete

And I'm using that method here:

	public class MainDbContext : DbContext
	{
		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);
			// ...

			// Map JSON string properties to the native type jsonb.
			foreach (var entity in modelBuilder.Model.GetEntityTypes())
			{
				foreach (var property in entity.GetProperties())
				{
					if (property.GetColumnName().EndsWith("Json") &&
						property.ClrType == typeof(string))
					{
						property.SetColumnType("jsonb");
					}
				}
			}
		}
	}

This doesn't compile anymore. The migration document has a code sample that doesn't look right to me. I don't want to look up anything in "Users" here but I need the column of a table. (Are there other columns, too?!) The documentation of the method itself is not helpful as it does not explain what I should pass as parameter.

How can I make that code work again with the current EF version? I want that method back and I don't understand why it was removed. It has always worked perfectly for me. This change is not an improvement for me.

EF Core version: 6.0
Database provider: PostgreSQL
Target framework: .NET 6.0
Operating system: Windows 10, Linux
IDE: Visual Studio 2022

@Applesauce314
Copy link

I had basically the same loop in my code, since I was not using the multiple mapping that the caused the function to be obsolete, I was able to add the line below.

var table = Microsoft.EntityFrameworkCore.Metadata.StoreObjectIdentifier.Create(entityType, 
Microsoft.EntityFrameworkCore.Metadata.StoreObjectType.Table);

and then call GetColumnName with table.Value as an argument.

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
  var table = Microsoft.EntityFrameworkCore.Metadata.StoreObjectIdentifier.Create(entityType, 
  Microsoft.EntityFrameworkCore.Metadata.StoreObjectType.Table);
  if (table != null)
  {
      foreach (var property in entity.GetProperties())
      {
          if (property.GetColumnName(table.Value).EndsWith("Json") && property.ClrType == typeof(string))
          {
              property.SetColumnType("jsonb");
          }
      }
  }
}

Obviously this will not continue to work if you do the double mapping thing described, (and have different column names for the same property), but for most use cases , including yours since you are migrating and won't have the double mapping thing, it will work just fine.

The "Users" table in the example is just the "table" the column in the example is located on.

Applesauce314 referenced this issue in Applesauce314/EntityFramework.Docs Sep 6, 2022
Wording for provided example does not make it seem like and example "Use the following code to get the column name for a specific table:"
see [https://github.com/dotnet/efcore/issues/28983]
@ajcvickers
Copy link
Contributor

Note for triage: I believe that prior to EF7, GetColumnBaseName() could be used. Starting with EF7, GetColumnName() can be used again. It now has the semantics of GetColumnBaseName(), but this is identical to the semantics of the original GetColumnName() when not using any kind of table splitting.

@ajcvickers ajcvickers transferred this issue from dotnet/efcore Sep 12, 2022
@ajcvickers ajcvickers self-assigned this Sep 12, 2022
@ygoe
Copy link
Author

ygoe commented Sep 15, 2022

Looks like this simpler alternative also works:

property.GetColumnName(StoreObjectIdentifier.Table(entity.GetTableName()))

Funny that this breaking change is expected to be reverted again. 🙄 I still don't understand why it was considered necessary in the first place.

@ajcvickers ajcvickers modified the milestones: 6.0.0, 7.0.0 Sep 21, 2022
ajcvickers added a commit that referenced this issue Sep 21, 2022
ajcvickers added a commit that referenced this issue Sep 21, 2022
ajcvickers added a commit that referenced this issue Sep 21, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants