简体   繁体   English

如何获得“实体框架代码优先”迁移来查看我的模型?

[英]How do I get Entity Framework Code First Migrations to see my models?

I made a new ASP.Net Web Application and enabled migrations on it. 我制作了一个新的ASP.Net Web应用程序,并在其上启用了迁移。 I ran add-migration initial and the initial migration does in fact have all the default tables for authentication (dbo.AspNetRoles, dbo.AspNetUserRoles, etc). 我运行了add-migration initial ,实际上initial迁移确实具有用于身份验证的所有默认表(dbo.AspNetRoles,dbo.AspNetUserRoles等)。 However, when I create my own context and add an entity model to it, I can't get migrations to acknowledge that model. 但是,当我创建自己的上下文并向其中添加实体模型时,我无法进行迁移以确认该模型。 That is, when I run add-migration added-watchedgame-model I just get an "empty" migration file. 也就是说,当我运行add-migration added-watchedgame-model我只会得到一个“空”迁移文件。 So what am I doing wrong? 那我在做什么错? Does my DbContext have to be referenced somehow? 是否必须以某种方式引用我的DbContext? can Entity Framework only handle migrations for 1 dbcontext? 实体框架只能处理1个dbcontext的迁移吗?

ReleaseDateMailerDBContext.cs: ReleaseDateMailerDBContext.cs:

using System.Data.Entity;
using WebApplication4.Models;

namespace WebApplication4.DataAccess
{
    public class ReleaseDateMailerDBContext : DbContext
    {
        public ReleaseDateMailerDBContext() : base("DefaultConnection") { }

        public DbSet<WatchedGameModel> WatchedGameModelSet { get; set; }
    }
}

WatchedGameModel.cs: WatchedGameModel.cs:

using System.ComponentModel.DataAnnotations;

namespace WebApplication4.Models
{
    public class WatchedGameModel
    {
        public int ID { get; set; }
        [MaxLength(1024)]
        public string URL { get; set; }
        public string Email { get; set; }
        public bool EmailSent { get; set; }
    }
}

"empty" migration file: “空”迁移文件:

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

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

        public override void Down()
        {
        }
    }
}

"Batch Clean" may resolve your porblem. “批量清洁”可能会解决您的问题。

It suggests that the EF tooling/assemblies are looking in a location other than the default build output location (typically /bin/Debug). 这表明EF工具/程序集在默认生成输出位置(通常是/ bin / Debug)之外的其他位置查找。 The clean command also, incidentally, clears intermediary outputs. 顺便说一句,clean命令还清除中间输出。

To do a batch clean: 要进行批量清洁:

  1. Select Build -> Batch Build 选择Build -> Batch Build
  2. Click Select All 单击Select All
  3. Click Clean 单击Clean

Close dialog, rebuild and re-attempt migration. 关闭对话框,重建并重新尝试迁移。

While running the add-migration command your package manager console should be pointed to the project having your DBContext class (WebApplication4.DataAccess). 运行add-migration命令时,应将程序包管理器控制台指向具有DBContext类(WebApplication4.DataAccess)的项目。 If you have migration in a different project than your web application project (suppose WebApplication4.Web) then you should run the following command: 如果要在与Web应用程序项目(假设为WebApplication4.Web)不同的项目中进行迁移,则应运行以下命令:

add-migration "MigrationName" -projectName:WebApplication.DataAccess -startupProjectName:WebApplication4.Web

Hope it helps!! 希望能帮助到你!!

  • With the built-in asp.net mvc project, a DbContext class ( ApplicationDbContext ) is already created! 使用内置的asp.net mvc项目,已经创建了一个DbContext类( ApplicationDbContext )!
  • When you enter enable-migrations , a migration configuration class is created based on the dbcontext class that it finds. 输入enable-migrations ,将基于找到的dbcontext类创建迁移配置类。
  • When you enter add-migration "migrationname" , That dbcontext class is what is checked for differences. 输入add-migration "migrationname" ,将检查该dbcontext类是否存在差异。

So all one has to do is, rather than making one's own class that derives from DbContext , use that one. 因此,所有要做的就是,不要使用从DbContext派生的自己的类, DbContext使用该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架6:如何使用代码优先迁移和FTP部署重置数据库 - Entity Framework 6: How do I reset a database using code first migrations and an FTP deploy 实体框架代码第一次迁移 - Entity Framework Code First Migrations 实体框架代码优先迁移添加了我的模型中不存在的字段 - Entity Framework Code First migrations adds a field not present in my model 如何在实体框架中进行迁移? - How do I make migrations in Entity Framework? 实体框架代码第一次迁移:以编程方式获取sql脚本 - Entity Framework Code First Migrations: get sql scripts programmatically 实体框架:如何在“代码优先”迁移中禁用模型兼容性检查 - Entity Framework: How to disable model compatibility check in Code First Migrations 如何在实体框架代码优先迁移中设置小数精度和小数位数 - How to set decimal precision and scale in entity framework code first migrations 如何正确地促进实体框架代码优先迁移到生产中? - how to properly promote Entity Framework code-first migrations into production? 如何更新实体框架7迁移和数据库 - 代码优先 - How to update entity framework 7 migrations and database - Code first 实体框架多对多-代码优先-迁移 - Entity Framework many to many - Code First - Migrations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM