EF迁移显示空的Up()Down()方法 [英] EF migration shows empty Up() Down() methods

查看:351
本文介绍了EF迁移显示空的Up()Down()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地数据库,目前是第二个版本,现在应该是第三个版本。



以前迁移的代码是由另一个程序员生成的所以我假设我在这里做错了。



在我的模型中有大约30个类,在模型文件夹里面有一个映射文件夹,它包含映射对于这30个类。



所以现在我添加了一个新的类,就像以前的类一样,然后在Package Manager Console中运行add-migration命令。 / p>

幸而我得到一个空的迁移Up()和Down()方法。



当我查看数据库有一个__migrationHistory可用于之前的2次迁移。如果我现在运行我的应用程序,还会添加第三个迁移,但显然新表不会被创建,因为它不在Up()方法中。



做错了?



我认为在脚手架以前的迁移时出现了一些问题...就像没有找到我添加的新的Code-First类。



这是我的命令:

  add-migration1.2 -verbose -ProjectNameMyEFproject

我假设脚手架不知道在哪里寻找新课程...或按惯例,所有模型类只是预期在项目中?



添加迁移结果:

 命名空间MyProject.Migrations 
{
using System;
使用System.Data.Entity.Migrations;

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

public override void Down()
{
}
}
}

新模型类的示例:

  using System; 
使用System.Collections.Generic;

命名空间MyProject.Models
{
public partial class MyTable
{

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


}
}

新的Mapping类

 使用System.ComponentModel.DataAnnotations.Schema; 
使用System.Data.Entity.ModelConfiguration;

命名空间MyProject.Models.Mapping
{
public class MyTableMap:EntityTypeConfiguration&MyTable>
{

public MyTableMap()
{
//主键
this.HasKey(t => t.SomeId);

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



//表&列映射
this.ToTable(MyTable,database);
this.Property(t => t.SomeId).HasColumnName(SomeId);
this.Property(t => t.SomeText).HasColumnName(SomeText);


}




}
}

谢谢,

解决方案

您需要添加你的表执行 DbContext 类,例如

  public class MyDatabaseEntities:DbContext {
public virtual DbSet&MyTable> MyTable {get;组; }
}


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,

解决方案

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

这篇关于EF迁移显示空的Up()Down()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆