Browse Source

Handle nullable type.

pull/5451/head
maliming 5 years ago
parent
commit
356dcb6648
  1. 13
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 13
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs
  3. 3
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs
  4. 2
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs
  5. 9
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs

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

@ -23,6 +23,7 @@ using Volo.Abp.EntityFrameworkCore.EntityHistory;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.EntityFrameworkCore.ValueConverters;
using Volo.Abp.Guids;
using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Reflection;
@ -340,13 +341,19 @@ namespace Volo.Abp.EntityFrameworkCore
{
if (TypeHelper.IsPrimitiveExtended(entryProperty.Metadata.ClrType, includeEnums: true))
{
if (entryProperty.Metadata.ClrType == typeof(Guid))
var conversionType = entryProperty.Metadata.ClrType;
if (TypeHelper.IsNullable(conversionType))
{
entryProperty.CurrentValue = TypeDescriptor.GetConverter(entryProperty.Metadata.ClrType).ConvertFromInvariantString(entityProperty.ToString());
conversionType = conversionType.GetFirstGenericArgumentIfNullable();
}
if (conversionType == typeof(Guid))
{
entryProperty.CurrentValue = TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(entityProperty.ToString());
}
else
{
entryProperty.CurrentValue = Convert.ChangeType(entityProperty, entryProperty.Metadata.ClrType, CultureInfo.InvariantCulture);
entryProperty.CurrentValue = Convert.ChangeType(entityProperty, conversionType, CultureInfo.InvariantCulture);
}
}
}

13
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using Volo.Abp.DynamicProxy;
using Volo.Abp.Localization;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Reflection;
@ -33,12 +34,18 @@ namespace Volo.Abp.Data
if (TypeHelper.IsPrimitiveExtended(typeof(TProperty), includeEnums: true))
{
if (typeof(TProperty) == typeof(Guid))
var conversionType = typeof(TProperty);
if (TypeHelper.IsNullable(conversionType))
{
return (TProperty)TypeDescriptor.GetConverter(typeof(TProperty)).ConvertFromInvariantString(value.ToString());
conversionType = conversionType.GetFirstGenericArgumentIfNullable();
}
return (TProperty)Convert.ChangeType(value, typeof(TProperty), CultureInfo.InvariantCulture);
if (conversionType == typeof(Guid))
{
return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString());
}
return (TProperty)Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture);
}
throw new AbpException("GetProperty<TProperty> does not support non-primitive types. Use non-generic GetProperty method and handle type casting manually.");

3
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs

@ -29,6 +29,7 @@ namespace Volo.Abp.EntityFrameworkCore.Domain
london.ExtraProperties["PhoneCode"] = 123456;
london.ExtraProperties["Rank"] = "88";
london.ExtraProperties["ZipCode"] = null;
london.ExtraProperties["Established"] = DateTime.MinValue;
london.ExtraProperties["Guid"] = "a7ae2efe-d8d6-466b-92e3-da14aa6e1c5b";
await CityRepository.UpdateAsync(london);
@ -36,10 +37,10 @@ namespace Volo.Abp.EntityFrameworkCore.Domain
london2.GetProperty<string>("PhoneCode").ShouldBe("123456");
london2.GetProperty<int>("Rank").ShouldBe(88);
london2.GetProperty<string>("ZipCode").ShouldBe(null);
london2.GetProperty<DateTime?>("Established").ShouldBe(DateTime.MinValue);
london2.GetProperty<Guid>("Guid").ShouldBe(new Guid("a7ae2efe-d8d6-466b-92e3-da14aa6e1c5b"));
}
[Fact]
public async Task An_Extra_Property_Configured_As_Extension2()
{

2
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/TestEntityExtensionConfigurator.cs

@ -25,6 +25,8 @@ namespace Volo.Abp.EntityFrameworkCore.Domain
"ZipCode"
).MapEfCoreProperty<City, int>(
"Rank"
).MapEfCoreProperty<City, DateTime?>(
"Established"
).MapEfCoreProperty<City, Guid>(
"Guid"
);

9
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HasExtraPropertiesExtensions_Tests.cs

@ -33,8 +33,17 @@ namespace Volo.Abp.TestApp.Testing
city.GetProperty<bool>("IsHot").ShouldBeFalse();
city.GetProperty<bool>("IsHot", true).ShouldBeTrue();
city.GetProperty<bool?>("IsHot?").ShouldBeNull();
city.GetProperty<bool?>("IsHot?", true).Value.ShouldBeTrue();
city.SetProperty("Guid", "2260AFEC-BBFD-42D4-A91A-DCB11E09B17F");
city.GetProperty<Guid>("Guid").ShouldBe(new Guid("2260AFEC-BBFD-42D4-A91A-DCB11E09B17F"));
city.SetProperty("Guid?", "2260AFEC-BBFD-42D4-A91A-DCB11E09B17F");
city.GetProperty<Guid?>("Guid?").ShouldBe(new Guid("2260AFEC-BBFD-42D4-A91A-DCB11E09B17F"));
city.SetProperty("DateTime?", DateTime.MinValue);
city.GetProperty<DateTime?>("DateTime?").ShouldBe(DateTime.MinValue);
}
}
}

Loading…
Cancel
Save