Get a script to create the database, including initialization data

I wonder if it is possible with Entity Framework Migrations to get an sql script to create the contents of my database, including all the data from my seed method in the Configuration class:

protected override void Seed(Sotasa.DAL.SqlContext context)
{
    //Data I'd like to be included to the script
}

The help of the Update-Database command does not look like this:

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName 
<String>] [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> [<CommonParameters>]
+5
source share
2 answers

It's impossible. (To have the data generated in the Seed method generating SQL queries).

An alternative workaround is to use a script to build -> create a database -> a database script with data? (Perhaps Powershell can do this by clicking on sql server management objects.)

0

, , , ( , CI-):

public class Configuration : DbMigrationsConfiguration<MyContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator(OracleConnectionInfo.InvariantName, new OracleEntityMigrationSqlGenerator());
    }

    protected override void Seed(MyContext context)
    {
        Logger.Write("In Seed method. User: " + userName);
        MyInitializer.AddUpdateDataSql(context);
    }
}

public class MyInitializer : MigrateDatabaseToLatestVersion<MyContext, Configuration>
{
    private const string Schemaname = "MYSCHEMA";

    public static void AddUpdateDataSql(Pronova2Context context)
    {
        DeleteTableData(context);
        DropCreateSequences(context);
        PopulateTypeTypeData(context);
        // etc.
    }

    private static void DeleteTableData(DbContext context)
    {
        context.Database.ExecuteSqlCommand(@"delete " + Schemaname + ".T_TABLE1"); }
        context.Database.ExecuteSqlCommand(@"delete " + Schemaname + ". T_TABLE2"); }
        // etc.
    }

    private static void DropCreateSequences(DbContext context)
    {
        context.Database.ExecuteSqlCommand(@"DROP SEQUENCE  " + Schemaname + ".T_TABLE1_SEQ"); }
        context.Database.ExecuteSqlCommand(@"DROP SEQUENCE  " + Schemaname + ".T_TABLE2_SEQ"); }
        // etc.
    }

    private static void PopulateTypeTypeData(DbContext context)
    {
        try { context.Database.ExecuteSqlCommand(@"Insert into " + Schemaname + ".T_TYPE_TYPE (TYPE_TYPE_ID,NAME,DESCRIPTION) values (1,'TypeType1','Name 1')");
        try { context.Database.ExecuteSqlCommand(@"Insert into " + Schemaname + ".T_TYPE_TYPE (TYPE_TYPE_ID,NAME,DESCRIPTION) values (2,'TypeType1','Name 2')");
        // etc.
    }
}
0

All Articles