Exemplo n.º 1
0
        protected virtual (byte[] body, string hash) CreateModelSnapshot(DbContextAccessorBase dbContextAccessor,
                                                                         out TypeNameCombiner modelSnapshotTypeName)
        {
            var typeName = ModelSnapshotCompiler.GenerateTypeName(dbContextAccessor.CurrentType);

            var result = ExtensionSettings.Preference.RunLocker(() =>
            {
                return(ModelSnapshotCompiler.CompileInMemory(dbContextAccessor,
                                                             dbContextAccessor.Model, typeName));
            });

            modelSnapshotTypeName = typeName;

            return(result);
        }
Exemplo n.º 2
0
        public static (byte[] body, string hash) CompileInMemory(DbContextAccessorBase accessor,
                                                                 IModel model, TypeNameCombiner typeName)
        {
            accessor.NotNull(nameof(accessor));
            model.NotNull(nameof(model));
            typeName.NotNull(nameof(typeName));

            var generator = accessor.GetService <IMigrationsCodeGenerator>();

            var sourceCode = generator.GenerateSnapshot(typeName.Namespace, accessor.CurrentType,
                                                        typeName.Name, model);

            var references = GetMetadataReferences(accessor.Dependency.Options, accessor.CurrentType);

            var buffer   = CSharpCompiler.CompileInMemory(references, sourceCode);
            var encoding = ExtensionSettings.Preference.DefaultEncoding;

            return(StoreAssembly(buffer), sourceCode.Sha256Base64String(encoding));
        }