Open Closed

"Rename" Id column #2162


User avatar
0
MichelZ created

Hi

Using abp 4.4 with EF/SQL Server.

We don't like using "Id" as the column name for our entities, so we are using this workaround: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/586#issuecomment-135599562

It does work fine for entities that don't have a Guid Key. Once the Key is Guid, things start to break down. EF throws errors that the "Id column was not found".

We have found the cause of it, it's AbpDbContext.TrySetGuidId and AbpDbContext.ConfigureValueGenerated

We have also found a suitable workaround by overriding these functions like this:

protected override void TrySetGuidId(EntityEntry entry, IEntity<Guid> entity)
        {
            if (entry.Metadata.FindProperty("Id") == null)
            {
                // Ignore this, it's because we have removed the Id property.
                return;
            }

            base.TrySetGuidId(entry, entity);
        }

        protected override void ConfigureValueGenerated<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
            where TEntity : class
        {
            if (!typeof(IEntity<Guid>).IsAssignableFrom(typeof(TEntity)))
            {
                return;
            }

            // Ignore if Id has NotMapped attribute
            var type = mutableEntityType.ClrType;
            var propertyInfo = type.GetProperty("Id", BindingFlags.DeclaredOnly |
                                                      BindingFlags.Public |
                                                      BindingFlags.Instance);

            if (propertyInfo != null)
            {
                var attribute = propertyInfo.GetCustomAttribute(typeof(NotMappedAttribute));
                if (attribute != null)
                {
                    return;
                }
            }

            base.ConfigureValueGenerated<TEntity>(modelBuilder, mutableEntityType);
        }

The configureValueGenerated function does create the property when it does not exist. So even when it was excluded with [NotMapped], this line from AbpDbContext adds it: var idPropertyBuilder = modelBuilder.Entity().Property(x => ((IEntity)x).Id);

(Note the documentation saying that "Returns an object that ban be used to configure a property of the entity type. If the specified property is not already part of the model, it will be added.")

So the Id property is added to the model if the type is Guid.

Then the TrySetGuidId also expects the Id property to be there: var idProperty = entry.Property("Id").Metadata.PropertyInfo;

Which of course fails, too.

Now to the question:

  • Is this the right way to handle this situation, or do you see a better way?
  • Are there some improvements for ABP that you can do to better support this scenario?

Thanks Michel


1 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi MichelZ

    I don't recommend you to do this, the disadvantages outweigh the advantages.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11