예제 #1
0
        // <summary>
        // Calculates an SHA256 hash of the EDMX from the given code first model. This is the hash stored in
        // the database in the EdmMetadata table in EF 4.1/4.2. The hash is always calculated using a v2 schema
        // as was generated by EF 4.1/4.2 and with the <see cref="EdmMetadata" /> entity included in the model.
        // </summary>
        public virtual string Calculate(DbCompiledModel compiledModel)
        {
            DebugCheck.NotNull(compiledModel);
            DebugCheck.NotNull(compiledModel.ProviderInfo);
            DebugCheck.NotNull(compiledModel.CachedModelBuilder);

            var providerInfo = compiledModel.ProviderInfo;
            var modelBuilder = compiledModel.CachedModelBuilder.Clone();

            // Add back in the EdmMetadata class because the hash created by EF 4.1 and 4.2 will contain it.
            EdmMetadataContext.ConfigureEdmMetadata(modelBuilder.ModelConfiguration);

            var databaseMetadata = modelBuilder.Build(providerInfo).DatabaseMapping.Database;

            databaseMetadata.SchemaVersion = XmlConstants.StoreVersionForV2; // Ensures SSDL version matches that created by EF 4.1/4.2

            var stringBuilder = new StringBuilder();

            using (var xmlWriter = XmlWriter.Create(
                       stringBuilder, new XmlWriterSettings
            {
                Indent = true
            }))
            {
                new SsdlSerializer().Serialize(
                    databaseMetadata,
                    providerInfo.ProviderInvariantName,
                    providerInfo.ProviderManifestToken,
                    xmlWriter);
            }

            return(ComputeSha256Hash(stringBuilder.ToString()));
        }
예제 #2
0
        public virtual string QueryForModelHash(
            Func <DbConnection, EdmMetadataContext> createContext)
        {
            DbConnection connection = this.CreateConnection();

            try
            {
                using (EdmMetadataContext edmMetadataContext = createContext(connection))
                {
                    if (this._existingTransaction != null)
                    {
                        if (this._existingTransaction.Connection == connection)
                        {
                            edmMetadataContext.Database.UseTransaction(this._existingTransaction);
                        }
                    }
                    try
                    {
                        return(edmMetadataContext.Metadata.AsNoTracking <EdmMetadata>().OrderByDescending <EdmMetadata, int>((Expression <Func <EdmMetadata, int> >)(m => m.Id)).FirstOrDefault <EdmMetadata>()?.ModelHash);
                    }
                    catch (EntityCommandExecutionException ex)
                    {
                        return((string)null);
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
예제 #3
0
        private static void DropMigrationHistoryAndAddEdmMetadata(DbConnection connection, string hash)
        {
            using (var poker = new EdmMetadataContext(connection, contextOwnsConnection: false))
            {
                poker.Database.ExecuteSqlCommand("drop table " + HistoryContext.TableName);

                poker.Database.ExecuteSqlCommand(
                    ((IObjectContextAdapter)poker).ObjectContext.CreateDatabaseScript());

#pragma warning disable 612,618
                poker.Metadata.Add(
                    new EdmMetadata
                        {
                            ModelHash = hash
                        });
#pragma warning restore 612,618

                poker.SaveChanges();
            }
        }
예제 #4
0
        public virtual string Calculate(DbCompiledModel compiledModel)
        {
            DbProviderInfo providerInfo   = compiledModel.ProviderInfo;
            DbModelBuilder dbModelBuilder = compiledModel.CachedModelBuilder.Clone();

            EdmMetadataContext.ConfigureEdmMetadata(dbModelBuilder.ModelConfiguration);
            EdmModel database = dbModelBuilder.Build(providerInfo).DatabaseMapping.Database;

            database.SchemaVersion = 2.0;
            StringBuilder     stringBuilder = new StringBuilder();
            StringBuilder     output        = stringBuilder;
            XmlWriterSettings settings      = new XmlWriterSettings()
            {
                Indent = true
            };

            using (XmlWriter xmlWriter = XmlWriter.Create(output, settings))
                new SsdlSerializer().Serialize(database, providerInfo.ProviderInvariantName, providerInfo.ProviderManifestToken, xmlWriter, true);
            return(ModelHashCalculator.ComputeSha256Hash(stringBuilder.ToString()));
        }
예제 #5
0
 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     EdmMetadataContext.ConfigureEdmMetadata(modelBuilder.ModelConfiguration);
 }