Last active
October 18, 2016 08:32
-
-
Save Antaris/ca0f271311fdb84ef3a8db95b149a307 to your computer and use it in GitHub Desktop.
Early work on custom type conversions for Entity Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogDbContext : DbContext | |
{ | |
public DbSet<BlogPost> Posts { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPost | |
{ | |
public int Id { get; set; } | |
public TagSet Tags { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomEntityMaterializerSource : EntityMaterializerSource | |
{ | |
public CustomEntityMaterializerSource(IMemberMapper memberMapper) : base(memberMapper) | |
{ | |
} | |
public override Expression CreateReadValueExpression(Expression valueBuffer, Type type, int index) | |
{ | |
if (type == typeof(TagSet)) | |
{ | |
return Expression.Convert(base.CreateReadValueExpression(valueBuffer, typeof(string), index), typeof(TagSet)); | |
} | |
return base.CreateReadValueExpression(valueBuffer, type, index); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomSqlServerCompositeMethodCallTranslator : SqlServerCompositeMethodCallTranslator | |
{ | |
private static readonly IMethodCallTranslator[] _methodCallTranslators = | |
{ | |
new TagSetContainsTranslator() | |
}; | |
public CustomSqlServerCompositeMethodCallTranslator(ILogger<SqlServerCompositeMethodCallTranslator> logger) : base(logger) | |
{ | |
AddTranslators(_methodCallTranslators); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomSqlServerTypeMapper : SqlServerTypeMapper | |
{ | |
private readonly RelationalTypeMapping _tags | |
= new SqlServerMaxLengthMapping("nvarchar(1000)", typeof(TagSet), dbType: System.Data.DbType.String, unicode: true, size: 1000); | |
protected override RelationalTypeMapping FindCustomMapping(IProperty property) | |
{ | |
if (property.ClrType == typeof(TagSet)) | |
{ | |
return _tags; | |
} | |
return base.FindCustomMapping(property); | |
} | |
public override RelationalTypeMapping FindMapping(Type clrType) | |
{ | |
if (clrType == typeof(TagSet)) | |
{ | |
return _tags; | |
} | |
return base.FindMapping(clrType); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSingleton<IRelationalTypeMapper, CustomSqlServerTypeMapper>(); | |
services.AddSingleton<SqlServerTypeMapper, CustomSqlServerTypeMapper>(); | |
services.AddSingleton<IEntityMaterializerSource, CustomEntityMaterializerSource>(); | |
services.AddScoped<SqlServerCompositeMethodCallTranslator, CustomSqlServerCompositeMethodCallTranslator>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: