Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF migration shows empty Up() Down() methods

I have a local database that is currently in it's second version and should now go to it's third version.

The code for the previous migrations was generated by another programmer so I am assuming I am doing something wrong here.

In my model there are around 30 classes, and inside the model folder there is a mapping folder and it contains the mappings for those 30 classes.

So now I added 1 new class in the same manner as those previous classes and then run the add-migration command in the Package Manager Console.

Infortunately I get an empty migration Up() and Down() method.

When I look in the database there is a __migrationHistory available with the previous 2 migrations. If I run my application now, the third migration is also added but obviously the new table is not being created because it's not in the Up() method.

What could I be doing wrong?

I think something is going wrong when scaffolding the previous migrations... It's like it can't find the new Code-First classes I have added.

This is my command:

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

I am assuming that the scaffolding doesn't know where to look for the new class... or is this by convention that all model classes are just expected to be in the project?

Result 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()     {     } } } 

Sample of new Model Class:

using System; using System.Collections.Generic;  namespace MyProject.Models { public partial class MyTable {      public string SomeId { get; set; }     public string SomeText { get; set; }   } } 

Sample of new Mapping class

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");       }        } } 

Thank you,

like image 620
user1841243 Avatar asked Mar 28 '14 09:03

user1841243


People also ask

How do you use down method in migration?

To use the Down method you must explicitly specify the target migration for your upgrade. If the target migration is the old one, the migration API will automatically use the Down method and downgrade your database.

How do I create an empty migration EF core?

In Package Manager Console in Visual Studio, add an empty migration targeting your Database context. It'll create an empty migration provided you don't have any other DB changes to be applied.


2 Answers

You need to add your table to your implementation of the DbContext class, e.g.

public class MyDatabaseEntities : DbContext {     public virtual DbSet<MyTable> MyTable { get; set; } } 
like image 142
CodingIntrigue Avatar answered Oct 09 '22 18:10

CodingIntrigue


While rolling back an existing EF Core Data Context back to empty, my migrations wouldn't generate until I removed the ApplicationDbContextModelSnapshot that accompanied the migrations.

This class is auto-generated and needs to align with your current migration level.

like image 28
Jeremy Smith Avatar answered Oct 09 '22 16:10

Jeremy Smith