Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 54a749b

Browse files
authored
Added support for ValueConverter. Preparing for 3.0.0.rc1 release. (#52)
* Added support for ValueConverter. * Updated to version 3.0.0-rc.1
1 parent 0d0351b commit 54a749b

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ static bool CompareProperty(MemberInfo property, MemberInfo memberInfo)
111111
{
112112
if (property == memberInfo)
113113
return true;
114+
115+
if (property == null)
116+
return false;
114117

115118
if (memberInfo.DeclaringType?.IsAssignableFrom(property.DeclaringType) == true
116119
&& memberInfo.Name == property.Name
@@ -214,9 +217,8 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
214217

215218
return associations.Select(a => (T)(Attribute)a).ToArray();
216219
}
217-
}
218-
219-
if (typeof(T) == typeof(Sql.ExpressionAttribute))
220+
}
221+
else if (typeof(T) == typeof(Sql.ExpressionAttribute))
220222
{
221223
// Search for translator first
222224
if (_dependencies != null)
@@ -260,10 +262,47 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
260262
}).ToArray();
261263
}
262264
}
265+
else if (typeof(T) == typeof(ValueConverterAttribute))
266+
{
267+
var et = _model?.FindEntityType(type);
268+
if (et != null)
269+
{
270+
var props = et.GetProperties();
271+
var prop = props.FirstOrDefault(p => CompareProperty(p, memberInfo));
272+
273+
var converter = prop?.GetValueConverter();
274+
if (converter != null)
275+
{
276+
var valueConverterAttribute = new ValueConverterAttribute
277+
{
278+
ValueConverter = new ValueConverter(converter.ConvertToProviderExpression,
279+
converter.ConvertFromProviderExpression, false)
280+
};
281+
return new T[] { (T) (Attribute) valueConverterAttribute };
282+
}
283+
}
284+
}
263285

264286
return Array.Empty<T>();
265287
}
266288

289+
class ValueConverter : IValueConverter
290+
{
291+
public ValueConverter(
292+
LambdaExpression convertToProviderExpression,
293+
LambdaExpression convertFromProviderExpression, bool handlesNulls)
294+
{
295+
FromProviderExpression = convertFromProviderExpression;
296+
ToProviderExpression = convertToProviderExpression;
297+
HandlesNulls = handlesNulls;
298+
}
299+
300+
public bool HandlesNulls { get; }
301+
public LambdaExpression FromProviderExpression { get; }
302+
public LambdaExpression ToProviderExpression { get; }
303+
304+
}
305+
267306
class SqlTransparentExpression : SqlExpression
268307
{
269308
public Expression Expression { get; }
@@ -329,7 +368,9 @@ private Sql.ExpressionAttribute GetDbFunctionFromProperty(Type type, PropertyInf
329368
{
330369
EFCoreExpressionAttribute result = null;
331370

332-
if ((propInfo.GetMethod?.IsStatic != true) && !mi.GetCustomAttributes<Sql.ExpressionAttribute>().Any())
371+
if ((propInfo.GetMethod?.IsStatic != true)
372+
&& !(mi is DynamicColumnInfo)
373+
&& !mi.GetCustomAttributes<Sql.ExpressionAttribute>().Any())
333374
{
334375
var objExpr = new SqlTransparentExpression(Expression.Constant(DefaultValue.GetValue(type), type), _mappingSource?.FindMapping(propInfo));
335376

Source/LinqToDB.EntityFrameworkCore/linq2db.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="linq2db" Version="3.0.0-rc.0" />
12+
<PackageReference Include="linq2db" Version="3.0.0-rc.1" />
1313
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
1414
</ItemGroup>
1515

Tests/LinqToDB.EntityFrameworkCore.BaseTests/LinqToDB.EntityFrameworkCore.BaseTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="linq2db" Version="3.0.0-rc.2322" />
1415
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
1516
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
1617
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.3" />

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/JsonConverTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public void TestJsonConvert()
9090
{
9191
LinqToDBForEFTools.Initialize();
9292

93-
// converting from string, because usually JSON is stored as string, but it depends on DataProvider
94-
Mapping.MappingSchema.Default.SetConverter<string, LocalizedString>(v => JsonConvert.DeserializeObject<LocalizedString>(v));
95-
96-
// here we told linq2db how to pass converted value as DataParameter.
97-
Mapping.MappingSchema.Default.SetConverter<LocalizedString, DataParameter>(v => new DataParameter("", JsonConvert.SerializeObject(v), LinqToDB.DataType.NVarChar));
93+
// // converting from string, because usually JSON is stored as string, but it depends on DataProvider
94+
// Mapping.MappingSchema.Default.SetConverter<string, LocalizedString>(v => JsonConvert.DeserializeObject<LocalizedString>(v));
95+
//
96+
// // here we told linq2db how to pass converted value as DataParameter.
97+
// Mapping.MappingSchema.Default.SetConverter<LocalizedString, DataParameter>(v => new DataParameter("", JsonConvert.SerializeObject(v), LinqToDB.DataType.NVarChar));
9898

9999
using (var ctx = new JsonConvertContext(_options))
100100
{

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/Models/Northwind/NorthwindContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void ConfigureGlobalQueryFilters(ModelBuilder builder)
5757
if (typeof(ISoftDelete).IsSameOrParentOf(entityType.ClrType))
5858
{
5959
var method = ConfigureEntityFilterMethodInfo.MakeGenericMethod(entityType.ClrType);
60-
method.Invoke(this, new object?[] { builder });
60+
method.Invoke(this, new object[] { builder });
6161
}
6262
}
6363
}

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/ToolsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void SetIdentityInsert(DbContext ctx, string tableName, bool isOn)
5959
var str = $"SET IDENTITY_INSERT {tableName} " + (isOn ? "ON" : "OFF");
6060
try
6161
{
62-
ctx.Database.ExecuteSqlCommand(str);
62+
ctx.Database.ExecuteSqlRaw(str);
6363
}
6464
catch (Exception)
6565
{

0 commit comments

Comments
 (0)