Exemplo n.º 1
0
        public ToolingFixture()
        {
            var contextDir = IOHelpers.GetTempDirName();

            CreateContextProject(contextDir);

            var targetDir = IOHelpers.GetTempDirName();

            CreateMigrationsProject(targetDir, contextDir);
            AddAppConfig(targetDir);

            ProjectDir = targetDir;
            ContextDir = contextDir;
        }
Exemplo n.º 2
0
        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;
        }