Esempio n. 1
0
        public MongoDbContextModel Build()
        {
            lock (SyncObj)
            {
                var entityModels = _entityModelBuilders
                                   .Select(x => x.Value)
                                   .Cast <IMongoEntityModel>()
                                   .ToImmutableDictionary(x => x.EntityType, x => x);

                var baseClasses = new List <Type>();

                foreach (var entityModel in entityModels.Values)
                {
                    var map = entityModel.As <IHasBsonClassMap>().GetMap();
                    if (!BsonClassMap.IsClassMapRegistered(map.ClassType))
                    {
                        BsonClassMap.RegisterClassMap(map);
                    }

                    baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false));
                }

                baseClasses = baseClasses.Distinct().ToList();

                foreach (var baseClass in baseClasses)
                {
                    if (!BsonClassMap.IsClassMapRegistered(baseClass))
                    {
                        var map = new BsonClassMap(baseClass);
                        map.ConfigurePlusConventions();
                        BsonClassMap.RegisterClassMap(map);
                    }
                }

                return(new MongoDbContextModel(entityModels));
            }
        }
 public MongoEntityModelBuilder()
 {
     EntityType    = typeof(TEntity);
     _bsonClassMap = new BsonClassMap <TEntity>();
     _bsonClassMap.ConfigurePlusConventions();
 }