Browse Source

Ignore properties with `NotMappedAttribute`.

Resolve #14485
pull/14491/head
maliming 4 years ago
parent
commit
58847bf35e
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 16
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 4
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs
  3. 3
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DateTimeKind_Tests.cs

16
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -624,21 +624,17 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
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 =>
foreach (var property in mutableEntityType.GetProperties().
Where(property => property.PropertyInfo != null &&
(property.PropertyInfo.PropertyType == typeof(DateTime) || property.PropertyInfo.PropertyType == typeof(DateTime?)) &&
property.PropertyInfo.CanWrite &&
ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableDateTimeNormalizationAttribute>(property.PropertyInfo) == null))
{
modelBuilder
.Entity<TEntity>()
.Property(property.Name)
.HasConversion(dateTimeValueConverter);
});
}
}
}

4
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing;
@ -21,6 +22,9 @@ public class Person : FullAuditedAggregateRoot<Guid>, IMultiTenant
[DisableDateTimeNormalization]
public virtual DateTime? LastActive { get; set; }
[NotMapped]
public virtual DateTime? NotMappedDateTime { get; set; }
public virtual Collection<Phone> Phones { get; set; }
public virtual DateTime LastActiveTime { get; set; }

3
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DateTimeKind_Tests.cs

@ -41,5 +41,8 @@ public abstract class DateTimeKind_Tests<TStartupModule> : TestAppTestBase<TStar
person.LastActive.ShouldNotBeNull();
person.LastActive.Value.Kind.ShouldBe(DateTimeKind.Unspecified);
person.LastActive.Value.ToString("yyy-MM-dd HH:mm:ss").ShouldBe("2020-01-01 00:00:00");
//NotMappedDateTime [NotMappedAttribute]
person.NotMappedDateTime.ShouldBeNull();
}
}

Loading…
Cancel
Save