Exemplo n.º 1
0
        private void SetEmptyGuidKey(TEntity entity)
        {
            Type keyType = typeof(TKey);

            //自增int
            if (keyType == typeof(int))
            {
                IKeyGenerator <int> generator = _serviceProvider.GetService <IKeyGenerator <int> >();
                entity.Id = generator.Create().CastTo <TKey>();
                return;
            }
            //雪花long
            if (keyType == typeof(long))
            {
                IKeyGenerator <long> generator = _serviceProvider.GetService <IKeyGenerator <long> >();
                entity.Id = generator.Create().CastTo <TKey>();
            }
            //顺序guid
            if (keyType == typeof(Guid) && entity.Id.Equals(Guid.Empty))
            {
                DatabaseType             databaseType = _dbContext.GetDatabaseType();
                ISequentialGuidGenerator generator    =
                    _serviceProvider.GetServices <ISequentialGuidGenerator>().FirstOrDefault(m => m.DatabaseType == databaseType);
                entity.Id = generator == null?SequentialGuid.Create(databaseType).CastTo <TKey>() : generator.Create().CastTo <TKey>();
            }
        }