public static void SaveFacetValues <TEntity>(this TEntity entity, EntityDbContext entityDbContext)
            where TEntity : IEntity, IExtensible
        {
            var FacetData = entity.FindExtension <EntityFacetData>();

            if (FacetData == null)
            {
                return;
            }

            var FacetValueSet = entityDbContext.GetDataSet <FacetValue>();

            foreach (var key in FacetData.FacetValues.Keys.ToList())
            {
                var FacetValue = FacetData.FacetValues[key];

                if (FacetValue.Id == 0 && FacetValue.Value != null)
                {
                    FacetValue.FacetId  = key.Id;
                    FacetValue.EntityId = entity.Id;

                    FacetValueSet.Add(FacetValue);
                }
            }

            entityDbContext.SaveChanges();
        }
        public static void LoadFacetValues <TEntity>(this TEntity entity, EntityDbContext entityDbContext)
            where TEntity : IEntity, IExtensible
        {
            var FacetData = entity.FindExtension <EntityFacetData>();

            if (FacetData != null)
            {
                return;
            }

            var cachedEntityTypeSet = entityDbContext.GetCachedDataSet <CachedEntityType>();
            var cachedFacetset      = entityDbContext.GetCachedDataSet <CachedFacet>();
            var FacetValueSet       = entityDbContext.GetDataSet <FacetValue>();

            CachedEntityType entityType = cachedEntityTypeSet.GetByType <TEntity>();

            var Facets = cachedFacetset.GetAll()
                         .Where(a => a.EntityTypeId == entityType.Id)
                         .Where(a => a.DoQualifiersMatchForEntity(entity))
                         .ToList();

            var FacetIds = Facets.Select(a => a.Id)
                           .ToList();
            var FacetValues = FacetValueSet.Where(a => FacetIds.Contains(a.FacetId))
                              .ToList();

            var values = Facets.ToDictionary(
                a => a,
                a => FacetValues.FirstOrDefault(b => b.FacetId == a.Id) ?? FacetValueSet.Create());

            FacetData = new EntityFacetData(values);

            entity.AddOrReplaceExtension(FacetData);
        }
Example #3
0
        /// <summary>
        /// Called by application code early in the app life cycle to initialize
        /// all plugins. Will be called sometime after migrations have run.
        /// </summary>
        /// <param name="context">The database context the plugin can make changes in.</param>
        public override void Initialize(EntityDbContext context)
        {
            var entityTypes = context.EntityContextOptions.RegisteredEntities.Keys;
            var dataSet     = context.GetDataSet <EntityType>();

            foreach (var entityType in entityTypes)
            {
                EntityType entity        = null;
                var        guidAttribute = entityType.GetCustomAttribute <GuidAttribute>();
                Guid?      guid          = null;

                if (guidAttribute != null && Guid.TryParse(guidAttribute.Value, out var guidValue))
                {
                    guid   = guidValue;
                    entity = dataSet.GetByGuid(guid.Value);
                }
                else
                {
                    entity = dataSet.AsQueryable()
                             .Where(a => a.QualifiedName == entityType.AssemblyQualifiedName)
                             .FirstOrDefault();
                }

                if (entity == null)
                {
                    entity = new EntityType
                    {
                        Guid = guid ?? Guid.NewGuid()
                    };

                    dataSet.Add(entity);
                }

                entity.Name          = entityType.FullName;
                entity.QualifiedName = entityType.AssemblyQualifiedName;
            }

            context.SaveChanges();
        }