Reputation: 1650
I need to scaffold migration with add-migration
from Package Manager Console with my custom Migration base CustomMigration
which is derived from DbMigration
.
public partial class NewMigration: CustomMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
I can use different command if needed. I don't have any skills in powershell scripting. How can i achieve this?
Upvotes: 2
Views: 2152
Reputation: 1650
I Created new class which generated my migrations:
public class AuditMigrationCodeGenerator : CSharpMigrationCodeGenerator
{
protected override void WriteClassStart(string @namespace, string className, IndentedTextWriter writer, string @base, bool designer = false, IEnumerable<string> namespaces = null)
{
@base = @base == "DbMigration" ? "AuditMigration" : @base;
var changedNamespaces = namespaces?.ToList() ?? new List<string>();
changedNamespaces.Add("Your.Custom.Namespace");
base.WriteClassStart(@namespace, className, writer, @base, designer, changedNamespaces);
}
}
In Configuration.cs :
internal sealed class Configuration : DbMigrationsConfiguration<EfDataAccess>
{
public Configuration()
{
this.AutomaticMigrationsEnabled = false;
CodeGenerator = new AuditMigrationCodeGenerator();
}
}
And it will use your Custom code generator, which generates migrations with my desired custom migration base.
For more info: https://romiller.com/2012/11/30/code-first-migrations-customizing-scaffolded-code/
Upvotes: 7
Reputation: 2879
run command add-migration NewMigration
. It will add new migration with name "NewMigration". If there is no changes in model, the migration will be empty:
public partial class NewMigration : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
Change base class of NewMigration to CustomMigration:
public partial class NewMigration : CustomMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
update-database
to apply migrationUpvotes: 0