Ejemplo n.º 1
0
        public void Generate_when_database_out_of_date_throws()
        {
            DropDatabase();

            try
            {
                Database.SetInitializer(new DropCreateDatabaseAlways <ShopContext_v1>());

                using (var context = CreateContext <ShopContext_v1>())
                {
                    context.Database.Create();

                    DropMigrationHistoryAndAddEdmMetadata(
                        context.Database.Connection,
#pragma warning disable 612,618
                        EdmMetadata.TryGetModelHash(context));
#pragma warning restore 612,618

                    Assert.True(TableExists("EdmMetadata"));

                    var migrator = CreateMigrator <ShopContext_v1b>();

                    Assert.Equal(
                        Strings.MetadataOutOfDate,
                        Assert.Throws <MigrationsException>(() => migrator.Scaffold("GrowUp", null, ignoreChanges: false)).Message);
                }
            }
            finally
            {
                Database.SetInitializer(new CreateDatabaseIfNotExists <ShopContext_v1>());
            }

            Assert.False(TableExists(HistoryContext.DefaultTableName));
        }
Ejemplo n.º 2
0
        public void Upgrade_when_database_up_to_date_creates_bootstrap_history_record()
        {
            DropDatabase();

            try
            {
                Database.SetInitializer(new DropCreateDatabaseAlways <ShopContext_v1>());

                using (var context = CreateContext <ShopContext_v1>())
                {
                    context.Database.Create();

                    DropMigrationHistoryAndAddEdmMetadata(
                        context.Database.Connection,
#pragma warning disable 612,618
                        EdmMetadata.TryGetModelHash(context));
#pragma warning restore 612,618

                    Assert.True(TableExists("EdmMetadata"));

                    var migrator = CreateMigrator <ShopContext_v1>();

                    migrator.Update();
                }
            }
            finally
            {
                Database.SetInitializer(new CreateDatabaseIfNotExists <ShopContext_v1>());
            }

            Assert.True(TableExists(HistoryContext.DefaultTableName));
        }
Ejemplo n.º 3
0
        public void Generate_when_database_up_to_date_creates_empty_migration()
        {
            DropDatabase();

            try
            {
                Database.SetInitializer(new DropCreateDatabaseAlways <ShopContext_v1>());

                using (var context = CreateContext <ShopContext_v1>())
                {
                    context.Database.Create();

                    DropMigrationHistoryAndAddEdmMetadata(
                        context.Database.Connection,
#pragma warning disable 612,618
                        EdmMetadata.TryGetModelHash(context));
#pragma warning restore 612,618

                    Assert.True(TableExists("EdmMetadata"));

                    var migrator = CreateMigrator <ShopContext_v1>();

                    var generatedMigration = new MigrationScaffolder(migrator.Configuration).Scaffold("Empty");

                    Assert.Equal(300, generatedMigration.UserCode.Length);
                }
            }
            finally
            {
                Database.SetInitializer(new CreateDatabaseIfNotExists <ShopContext_v1>());
            }

            Assert.False(TableExists(HistoryContext.DefaultTableName));
        }
Ejemplo n.º 4
0
        public void TryGetModelHash_returns_null_when_used_with_Model_First_DbContext()
        {
            using (var context = new SimpleModelContext(new EntityConnection(SimpleModelEntityConnectionString)))
            {
#pragma warning disable 612,618
                Assert.Null(EdmMetadata.TryGetModelHash(context));
#pragma warning restore 612,618
            }
        }
Ejemplo n.º 5
0
        public void Model_hash_can_be_obtained_from_Code_First_context_even_when_EdmMetadata_is_not_mapped()
        {
            using (var context = new DefaultIncludeMetadataInModelContext())
            {
#pragma warning disable 612,618
                Assert.False(String.IsNullOrWhiteSpace(EdmMetadata.TryGetModelHash(context)));
#pragma warning restore 612,618
            }
        }
Ejemplo n.º 6
0
        private void SaveModelHashToDatabase(PayolaContext context, string modelHash, ObjectContext objectContext)
        {
            if (edmMetaData != null)
            {
                objectContext.Detach(edmMetaData);
            }

            edmMetaData = new EdmMetadata();
            context.Set <EdmMetadata> ().Add(edmMetaData);
            edmMetaData.ModelHash = modelHash;
            context.SaveChanges();
        }
Ejemplo n.º 7
0
        public void TryGetModelHash_returns_null_when_used_with_DbContext_created_from_existing_ObjectContext()
        {
            using (var outerContext = new SimpleModelContext())
            {
                using (var context = new SimpleModelContext(GetObjectContext(outerContext)))
                {
#pragma warning disable 612,618
                    Assert.Null(EdmMetadata.TryGetModelHash(context));
#pragma warning restore 612,618
                }
            }
        }
        private static void UseEdmMetadataTable()
        {
            using (var context = new BreakAwayContext())
            {
                var modelHash = EdmMetadata.TryGetModelHash(context);
                Console.WriteLine("Current Model Hash: {0}", modelHash);

                var databaseHash = context.Set <EdmMetadata>().Single().ModelHash;
                Console.WriteLine("Current Database Hash: {0}", databaseHash);

                var compatible = context.Database.CompatibleWithModel(throwIfNoMetadata: true);
                Console.WriteLine("Model Compatible With Database?: {0}", compatible);
            }
        }
        private bool CompatibleWithModel(string modelHash, DbContext context, ObjectContext objectContext)
        {
            var isEdmMetaDataInStore = objectContext.ExecuteStoreQuery <int>(LookupEdmMetaDataTable).FirstOrDefault();

            if (isEdmMetaDataInStore == 1)
            {
                _edmMetaData = context.Set <EdmMetadata>().FirstOrDefault();
                if (_edmMetaData != null)
                {
                    return(modelHash == _edmMetaData.ModelHash);
                }
            }
            return(false);
        }
Ejemplo n.º 10
0
        private void Model_hash_can_be_obtained_from_Code_First_context(bool initializeContext)
        {
            using (var context = new SimpleModelContext())
            {
                if (initializeContext)
                {
                    context.Database.Initialize(force: false);
                }

#pragma warning disable 612,618
                var hash = EdmMetadata.TryGetModelHash(context);
#pragma warning restore 612,618

                Assert.NotNull(hash);
            }
        }
Ejemplo n.º 11
0
        private bool CompatibleWithModel(string modelHash, DbContext context, ObjectContext objectContext)
        {
            var isEdmMetaDataInStore = objectContext.ExecuteStoreQuery <int> (@"
                Select COUNT(*) 
                FROM INFORMATION_SCHEMA.TABLES T 
                Where T.TABLE_NAME = 'EdmMetaData'").FirstOrDefault();

            if (isEdmMetaDataInStore == 1)
            {
                edmMetaData = context.Set <EdmMetadata> ().FirstOrDefault();
                if (edmMetaData != null)
                {
                    return(modelHash == edmMetaData.ModelHash);
                }
            }
            return(false);
        }
Ejemplo n.º 12
0
        private void Model_hash_can_be_obtained_from_Code_First_context_when_using_existing_model(bool initializeContext)
        {
            var model = SimpleModelContext.CreateBuilder().Build(ProviderRegistry.Sql2008_ProviderInfo).Compile();

            using (var context = new SimpleModelContext(model))
            {
                if (initializeContext)
                {
                    context.Database.Initialize(force: false);
                }

#pragma warning disable 612,618
                var hash = EdmMetadata.TryGetModelHash(context);
#pragma warning restore 612,618

                Assert.NotNull(hash);
            }
        }
Ejemplo n.º 13
0
        public void Model_hash_can_be_calculated_from_multiple_threads_using_a_single_DbCompiledModel()
        {
            var hashes = new ConcurrentBag <string>();

            ExecuteInParallel(
                () =>
            {
                using (var context = new SimpleModelContext())
                {
#pragma warning disable 612,618
                    var hash = EdmMetadata.TryGetModelHash(context);
#pragma warning restore 612,618

                    Assert.NotNull(hash);
                    hashes.Add(hash);
                }
            });

            Assert.True(hashes.All(h => hashes.First() == h));
        }
Ejemplo n.º 14
0
        public void TryGetModelHash_throws_when_given_null_context()
        {
#pragma warning disable 612,618
            Assert.Equal("context", Assert.Throws <ArgumentNullException>(() => EdmMetadata.TryGetModelHash(null)).ParamName);
#pragma warning restore 612,618
        }
Ejemplo n.º 15
0
 public static EdmMetadata CreateEdmMetadata(int ID)
 {
     EdmMetadata edmMetadata = new EdmMetadata();
     edmMetadata.Id = ID;
     return edmMetadata;
 }
Ejemplo n.º 16
0
 public void AddToEdmMetadatas(EdmMetadata edmMetadata)
 {
     base.AddObject("EdmMetadatas", edmMetadata);
 }