简体   繁体   中英

EF6 Code First Migration from Class Library Results in Blank Up/Down

I'm rewriting some code from EF5 into EF6. As part of the process I'm attempting to split my database context logic out into a class library. This worked before when part of the main project but now results in blank up/down methods when running add-migration .

public class SLDBContext : DbContext
{
    public SLDBContext()
        : base("name=SLApi")
    {
        System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<SLDBContext>());
    }

    public DbSet<Language> Languages { get; set; }
    public DbSet<Template> Templates { get; set; }
    public DbSet<TemplateFolder> TemplateFolders { get; set; }
    public DbSet<Element> Elements { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

Running add-migration MagicWombat with my class library selected in the Package Manager Console gives me this:

public partial class MagicWombat : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

My seed methods are empty but I would expect it to create me some empty tables non-the-less. Obviously I'm missing something, but what?

If your DbContext is no longer inside your startup project you need to use the -StartUpProjectName switch, so that EF can look at the config file there for the connection string.

add-migration MagicWombat -StartUpProjectName YourProject

if you've selected your class library as default in PM console, otherwise:

add-migration MagicWombat -ProjectName YouClassLibrary -StartUpProjectName YourProject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM