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

[NotMapped] doesn't work on DateTime properties #14485

Closed
devsharper opened this issue Oct 29, 2022 · 1 comment
Closed

[NotMapped] doesn't work on DateTime properties #14485

devsharper opened this issue Oct 29, 2022 · 1 comment
Assignees
Milestone

Comments

@devsharper
Copy link

ABP version: 6.0.1

DateTime properties with [NotMapped] attribute will still appear in EF Core model.

For example,

[Table("TestEntity")]
public class TestEntity : Entity<int>
{
    public int Hour { get; set; }

    [NotMapped]
    public DateTime MyTime
    {
        get => DateTime.Today.AddHours(Hour);
        set => Hour = value.Hour;
    }

    [NotMapped]
    public int MyHour
    {
        get => Hour;
        set => Hour = value;
    }
}


public class MyDbContext : AbpDbContext<MyDbContext>
{
    public DbSet<TestEntity> TestEntities { get; set; }

    //......

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<TestEntity>().ConfigureByConvention();

    }  //add breakpoint here, check the model generated
}

MyTime property is involved in model entity type, MyHour is not.

But EF Core add-migration command works correctly, so there is no MyTime column in database,

Invalid column name 'MyTime' exception will be thrown when querying.

I guess this is the issue? In AbpDbContext.cs :

    protected virtual void ConfigureValueConverter<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
        where TEntity : class
    {
        if (mutableEntityType.BaseType == null &&
            !typeof(TEntity).IsDefined(typeof(DisableDateTimeNormalizationAttribute), true) &&
            !typeof(TEntity).IsDefined(typeof(OwnedAttribute), true) &&
            !mutableEntityType.IsOwned())
        {
            if (LazyServiceProvider == null || Clock == null)
            {
                return;
            }

            var dateTimeValueConverter = new AbpDateTimeValueConverter(Clock);

            var dateTimePropertyInfos = typeof(TEntity).GetProperties()
                .Where(property =>
                    (property.PropertyType == typeof(DateTime) ||
                     property.PropertyType == typeof(DateTime?)) &&
                    property.CanWrite &&
                    ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableDateTimeNormalizationAttribute>(property) == null
                ).ToList();

            dateTimePropertyInfos.ForEach(property =>
            {
                modelBuilder
                    .Entity<TEntity>()
                    .Property(property.Name)
                    .HasConversion(dateTimeValueConverter);
            });
        }
    }

@devsharper
Copy link
Author

#13043

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

No branches or pull requests

2 participants