コード例 #1
0
        private static void CreateMigrationsProject(string targetDir, string contextDir)
        {
            var contextPath = Path.Combine(contextDir, "ContextLibrary1.dll");

            IOHelpers.CopyToDir(contextPath, targetDir);

            CreateProject(
                targetDir, "ClassLibrary1", new List <string> {
                contextPath
            },
                @"namespace ClassLibrary1
{
    using System.Data.Common;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.Migrations.History;

    public class Configuration : DbMigrationsConfiguration<ContextLibrary1.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
    }

    public class CustomHistoryContext : HistoryContext
    {
        public CustomHistoryContext(DbConnection existingConnection, string defaultSchema)
            : base(existingConnection, defaultSchema)
        {
        }
    }
}");
        }
コード例 #2
0
        private static void CreateProject(string targetDir, string targetName, List <string> additionalAssemblies, string code)
        {
            var targetFileName = targetName + ".dll";
            var targetPath     = Path.Combine(targetDir, targetFileName);

            var entityFrameworkPath = new Uri(typeof(DbContext).Assembly.CodeBase).LocalPath;

            IOHelpers.CopyToDir(entityFrameworkPath, targetDir);

            var entityFrameworkSqlServerPath = new Uri(typeof(SqlProviderServices).Assembly.CodeBase).LocalPath;

            IOHelpers.CopyToDir(entityFrameworkSqlServerPath, targetDir);

            using (var compiler = new CSharpCodeProvider())
            {
                additionalAssemblies.AddRange(
                    new List <string>
                {
                    "System.dll",
                    "System.Data.dll",
                    "System.Core.dll",
                    "System.Data.Entity.dll",
                    entityFrameworkPath
                });

                var results = compiler.CompileAssemblyFromSource(new CompilerParameters(additionalAssemblies.ToArray(), targetPath), code);

                if (results.Errors.HasErrors)
                {
                    throw new InvalidOperationException(results.Errors.Cast <CompilerError>().First(e => !e.IsWarning).ToString());
                }
            }
        }
コード例 #3
0
ファイル: ToolingScenarios.cs プロジェクト: jwanagel/jjwtest
        public ToolingFixture()
        {
            var          targetDir      = IOHelpers.GetTempDirName();
            const string targetName     = "ClassLibrary1";
            const string targetFileName = targetName + ".dll";
            var          targetPath     = Path.Combine(targetDir, targetFileName);

            var entityFrameworkPath = new Uri(typeof(DbContext).Assembly.CodeBase).LocalPath;

            IOHelpers.CopyToDir(entityFrameworkPath, targetDir);

            var entityFrameworkSqlServerPath = new Uri(typeof(SqlProviderServices).Assembly.CodeBase).LocalPath;

            IOHelpers.CopyToDir(entityFrameworkSqlServerPath, targetDir);

            using (var compiler = new CSharpCodeProvider())
            {
                var results
                    = compiler.CompileAssemblyFromSource(
                          new CompilerParameters(
                              new[]
                {
                    "System.dll",
                    "System.Data.dll",
                    "System.Core.dll",
                    "System.Data.Entity.dll",
                    entityFrameworkPath
                },
                              targetPath),
                          @"namespace ClassLibrary1
{
    using System.Data.Common;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.Migrations.History;

    public class Configuration : DbMigrationsConfiguration<Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
    }

    public class Context : DbContext
    {
        public Context()
            : base(""Name=ClassLibrary1"")
        {
        }

        public DbSet<Entity> Entities { get; set; }
    }

    public class CustomHistoryContext : HistoryContext
    {
        public CustomHistoryContext(DbConnection existingConnection, bool contextOwnsConnection, string defaultSchema)
            : base(existingConnection, contextOwnsConnection, defaultSchema)
        {
        }
    }

    public class Entity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}");

                if (results.Errors.HasErrors)
                {
                    throw new InvalidOperationException(results.Errors.Cast <CompilerError>().First(e => !e.IsWarning).ToString());
                }
            }

            var configurationFile = Path.Combine(targetDir, "App.config");

            File.WriteAllText(
                configurationFile,
                @"<?xml version='1.0' encoding='utf-8' ?>
<configuration>
  <connectionStrings>
    <add name='ClassLibrary1' connectionString='" +
                DatabaseProviderFixture.InitializeTestDatabase(DatabaseProvider.SqlClient, DatabaseProviderFixture.DefaultDatabaseName).
                ConnectionString +
                @"' providerName='System.Data.SqlClient' />
  </connectionStrings>
</configuration>");

            ProjectDir = targetDir;
        }