EF migration shows empty Up() Down() methods

c# database-migration entity-framework

Question

My local database is currently in its second iteration and is due to transition to its third.

I assume I'm doing something incorrect because another programmer created the code for the previous migrations.

There are about 30 classes in my model, and the mapping folder for those 30 classes is located inside the model folder.

Then I ran the add-migration command in the Package Manager Console after adding 1 new class in the same way as those earlier classes.

Unfortunately, the migration Up() and Down() methods provide empty results.

There is a __migrationHistory available with the past two migrations when I check in the database. The third migration is likewise added if I run my application right now, but the new table is obviously not generated because it is not in the Up() procedure.

What can I be doing incorrectly?

When scaffolding the earlier migrations, I believe there is a problem... It appears to be having trouble locating the new Code-First classes I've introduced.

This is my directive:

add-migration "1.2" -verbose -ProjectName "MyEFproject"

My assumption is that the scaffolding doesn't know where to look for the new class. However, is it customary for all model classes to be present in the project by default?

the outcome of add-migration

namespace MyProject.Migrations
{
using System;
using System.Data.Entity.Migrations;

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

    public override void Down()
    {
    }
}
}

A new Model Class illustration:

using System;
using System.Collections.Generic;

namespace MyProject.Models
{
public partial class MyTable
{

    public string SomeId { get; set; }
    public string SomeText { get; set; }


}
}

a new mapping class sample

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace MyProject.Models.Mapping
{
 public class MyTableMap : EntityTypeConfiguration<MyTable>
{

    public MyTableMap()
    {
        // Primary Key
        this.HasKey(t => t.SomeId);

        // Properties
        this.Property(t => t.SomeText)
            .IsRequired()
            .HasMaxLength(30);



        // Table & Column Mappings
        this.ToTable("MyTable", "database");
        this.Property(t => t.SomeId).HasColumnName("SomeId");
        this.Property(t => t.SomeText).HasColumnName("SomeText");


    }




   }
}

I'm grateful.

1
51
3/28/2014 9:16:11 AM

Accepted Answer

You must include your table in the execution of theDbContext class, as in.

public class MyDatabaseEntities : DbContext {
    public virtual DbSet<MyTable> MyTable { get; set; }
}
88
3/28/2014 9:29:53 AM

Popular Answer

By removing a copy of the last migration's record from the _MigrationHistory database, I was able to resolve this problem. Before I introduced DbSet for new model object to DbContext class, this record had been erroneously produced. Following this deletion, a new migration with the appropriate Up() and Down() methods was built.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow