forked from PGR/ascnet
1
0
Fork 0
ascnet/AscNet.Common/Database/DatabaseExtensions.cs

20 lines
829 B
C#
Raw Normal View History

2023-10-19 07:07:48 +00:00
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
namespace AscNet.Common.Database
{
public static class DatabaseExtensions
{
public static BsonClassMap<T> SetDictionaryRepresentation<T, TMember>(this BsonClassMap<T> classMap, Expression<Func<T, TMember>> memberLambda, DictionaryRepresentation representation)
{
var memberMap = classMap.GetMemberMap(memberLambda);
var serializer = memberMap.GetSerializer();
if (serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationSerializer)
serializer = dictionaryRepresentationSerializer.WithDictionaryRepresentation(representation);
memberMap.SetSerializer(serializer);
return classMap;
}
}
}