public async Task UpdateAsync()
        {
            // Arrange
            var database = new Database
            {
                Id   = Guid.NewGuid(),
                Name = databaseName
            };

            using var context = new AzureSqlDbContext(DbContextUtilities.GetContextOptions());
            await context.Databases.AddAsync(database);

            await context.SaveChangesAsync();

            var updatedName = "updatedTestDatabaseName";

            database.Name = updatedName;

            var databaseService = new SqlDatabaseService(context);

            // Act
            await databaseService.UpdateAsync(database);

            // Assert
            Assert.AreEqual(context.Databases.FirstOrDefault().Name, updatedName);
            Assert.AreEqual(context.Databases.FirstOrDefault(), database);
        }
 public void TestCompareSystemTypesUtilityMethod()
 {
     Assert.IsTrue(DbContextUtilities.CompareWithSystemType(typeof(DbSet <>), typeof(DbSet <>).FullName));
     Assert.IsFalse(DbContextUtilities.CompareWithSystemType(typeof(TestDomainServices.RoundtripOriginal_TestEntity), typeof(TestDomainServices.RoundtripOriginal_TestEntity).FullName));
     Assert.IsFalse(DbContextUtilities.CompareWithSystemType(null, typeof(DbSet <>).FullName));
     Assert.IsFalse(DbContextUtilities.CompareWithSystemType(typeof(DbSet <>), null));
 }
        /// <summary>
        /// Generates the MetadataWorkspace for the Data model. Tries to create the MetadataWorkspace from the csdl, ssdl and msl files. If they do not exist, then tries to
        /// instantiate the DbContext object. Also sets the _isCodeFirstModel field value.
        /// </summary>
        private void InitMetadataWorkspace()
        {
            if (this._isCodeFirstModel == null)
            {
                this._isCodeFirstModel = !this.TryInitMetadataWorkspaceFromResources();
            }

            // If metadataWorkspace cannot be obtained from the resources, new up the DbContext.
            if ((bool)this._isCodeFirstModel)
            {
                // First set the DatabaseInitializer to null. So do something to the effect of - System.Data.Entity.Database.SetInitializer<DbContextType>(null);
                DbContextUtilities.SetDbInitializer(this.ContextType, DbContextUtilities.DbContextTypeReference, null);

                // For CodeFirst mode, we need the DbContext type to have the default ctor.
                if (this.ContextType.GetConstructor(Type.EmptyTypes) == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.LinqToEntitiesDbContext_DefaultCtorNotFound, this.ContextType.FullName));
                }

                try
                {
                    // New up the DbContext.
                    object dbContext = Activator.CreateInstance(this.ContextType);
                    if (dbContext != null)
                    {
                        Type dbContextTypeReference = DbContextUtilities.GetDbContextTypeReference(this.ContextType);
                        System.Diagnostics.Debug.Assert(dbContextTypeReference != null, "If we have the DbContext type, then typeof(DbContext) should not be null.");
                        Type objectContextAdapter = DbContextUtilities.LoadTypeFromAssembly(dbContextTypeReference.Assembly, BusinessLogicClassConstants.IObjectContextAdapterTypeName);
                        if (objectContextAdapter != null)
                        {
                            PropertyInfo objectContextProperty = objectContextAdapter.GetProperty("ObjectContext");
                            if (objectContextProperty != null)
                            {
                                ObjectContext objectContext = objectContextProperty.GetValue(dbContext, null) as ObjectContext;
                                if (objectContext != null)
                                {
                                    this._metadataWorkspace = objectContext.MetadataWorkspace;
                                }
                            }
                        }
                    }
                }
                catch (TargetInvocationException tie)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.LinqToEntitiesDbContext_UnableToCreateContext),
                                                        tie == null ? null : tie.InnerException);
                }
                catch (Exception e)
                {
                    if (e.IsFatal())
                    {
                        throw;
                    }

                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.LinqToEntitiesDbContext_UnableToCreateContext), e);
                }
            }
            this._metadataWorkspace.LoadFromAssembly(this.ContextType.Assembly);
        }
Пример #4
0
 /// <summary>
 /// This method finds if the type is a set for the particular context type.
 /// For DbContext, it finds if the type is a DbSet.
 /// For ObjectContext, it finds if it is a ObjectSet;
 /// </summary>
 /// <param name="t">Type to be examined.</param>
 /// <returns><c>true</c> if the type is one of the 2 set types, and <c>false</c> otherwise</returns>
 private bool IsContextSetType(Type t)
 {
     if (this._isDbContext)
     {
         return(DbContextUtilities.CompareWithSystemType(t, BusinessLogicClassConstants.DbSetTypeName));
     }
     else
     {
         return(t.GetGenericTypeDefinition() == typeof(ObjectSet <>));
     }
 }
Пример #5
0
        public async Task SearchAsync()
        {
            // Arrange
            var table = new Table
            {
                Id         = Guid.NewGuid(),
                DatabaseId = Guid.NewGuid(),
                Name       = tableName,
                Schema     = JsonConvert.SerializeObject(new BrandTableSchema {
                    Name = StringType, Country = StringType
                })
            };

            var row1 = new Row
            {
                Id      = Guid.NewGuid(),
                TableId = table.Id,
                Content = JsonConvert.SerializeObject(new BrandTableSchema {
                    Name = row1Name, Country = row1Country
                })
            };

            var row2 = new Row
            {
                Id      = Guid.NewGuid(),
                TableId = table.Id,
                Content = JsonConvert.SerializeObject(new BrandTableSchema {
                    Name = row2Name, Country = row2Country
                })
            };

            using var context = new AzureSqlDbContext(DbContextUtilities.GetContextOptions());

            await context.Rows.AddAsync(row1);

            await context.Rows.AddAsync(row2);

            await context.Tables.AddAsync(table);

            await context.SaveChangesAsync();

            var rowService = new SqlRowService(context);

            // Act
            var result = await rowService.SearchByKeywordAsync(table.Id, searchKeyword, searchColumn);

            var resultResponse = result.Select(r => RowConverter.GetRowResponse(r)).ToList();

            // Assert
            Assert.IsNotNull(resultResponse);
            Assert.AreEqual(1, resultResponse.Count);
            Assert.AreEqual(row2Name, resultResponse.FirstOrDefault().Content["name"]);
        }
        public void TestDbContextUtilitiesMethods()
        {
            Type dbContextTypeRef = DbContextUtilities.GetDbContextTypeReference(typeof(EFCFNorthwindEntities));

            Assert.AreEqual(typeof(DbContext), dbContextTypeRef);

            Type dbContextType = DbContextUtilities.GetDbContextType(typeof(TestDomainServices.EFCF.Northwind));

            Assert.AreEqual(typeof(EFCFNorthwindEntities), dbContextType);

            Type dbSetType = DbContextUtilities.LoadTypeFromAssembly(typeof(DbContext).Assembly, typeof(DbSet <>).FullName);

            Assert.IsNotNull(dbSetType);
            Assert.AreEqual(dbSetType, typeof(DbSet <>));
        }
        public async Task CreateAsync()
        {
            // Arrange
            var database = new Database
            {
                Id   = Guid.NewGuid(),
                Name = databaseName
            };

            using var context = new AzureSqlDbContext(DbContextUtilities.GetContextOptions());

            var databaseService = new SqlDatabaseService(context);

            // Act
            await databaseService.CreateAsync(database);

            // Assert
            Assert.AreNotEqual(context.Databases.Count(), 0);
            Assert.AreEqual(context.Databases.FirstOrDefault(), database);
        }
        public async Task DeleteAsync()
        {
            // Arrange
            var database = new Database
            {
                Id   = Guid.NewGuid(),
                Name = databaseName
            };

            using var context = new AzureSqlDbContext(DbContextUtilities.GetContextOptions());
            await context.Databases.AddAsync(database);

            await context.SaveChangesAsync();

            var databaseService = new SqlDatabaseService(context);

            // Act
            await databaseService.DeleteAsync(database.Id);

            // Assert
            Assert.AreEqual(context.Databases.Count(), 0);
        }
Пример #9
0
        /// <summary>
        /// Gets a dictionary of all entity types associated with the current DbContext type.
        /// The key is the EntityType (CSpace) and the value is the CLR type of the generated entity.
        /// </summary>
        /// <returns>A dictionary keyed by entity type and returning corresponding CLR type</returns>
        private Dictionary <EntityType, Type> GetEntityTypesInContext()
        {
            Dictionary <EntityType, Type> entities       = new Dictionary <EntityType, Type>();
            ObjectItemCollection          itemCollection = this.MetadataWorkspace.GetItemCollection(DataSpace.OSpace) as ObjectItemCollection;

            foreach (EntityType objectSpaceEntityType in itemCollection.GetItems <EntityType>())
            {
                Type clrType = itemCollection.GetClrType(objectSpaceEntityType);

                // Skip the EF CodeFirst specific EdmMetadata type
                if (DbContextUtilities.CompareWithSystemType(clrType, BusinessLogicClassConstants.EdmMetadataTypeName))
                {
                    continue;
                }

                StructuralType edmEntityType;
                if (this.MetadataWorkspace.TryGetEdmSpaceType(objectSpaceEntityType, out edmEntityType))
                {
                    entities[(EntityType)edmEntityType] = clrType;
                }
            }
            return(entities);
        }
        public async Task GetAsync()
        {
            // Arrange
            var database = new Database
            {
                Id   = Guid.NewGuid(),
                Name = databaseName
            };

            using var context = new AzureSqlDbContext(DbContextUtilities.GetContextOptions());
            await context.Databases.AddAsync(database);

            await context.SaveChangesAsync();

            var databaseService = new SqlDatabaseService(context);

            // Act
            var result = await databaseService.GetAsync(database.Id);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(database, result);
        }