简体   繁体   English

Entity Framework Core Enable-Migrations 构建带有错误的 Configuration.cs 文件

[英]Entity Framework Core Enable-Migrations build the Configuration.cs file with Errors

I ran Enable-Migrations after much drama I finally got the command right so that it creates the Migrations directory within my project.在经历了很多戏剧性的事情之后,我运行了 Enable-Migrations 我终于得到了正确的命令,以便它在我的项目中创建 Migrations 目录。

However, this is the file it gave me back.. I have moved the Using Statements to the top and removed the ones that were invalid, but that is all I changed.但是,这是它给我的文件。我已将使用语句移到顶部并删除了无效的语句,但这就是我所做的全部更改。

有错误的配置

Raw Code.. Errors are in the Image link above.原始代码.. 错误在上面的图片链接中。

internal sealed class Configuration : DbMigrationsConfiguration<RapidDeploy.Models.BloggingContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(RapidDeploy.Models.BloggingContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

I have no idea where you went wrong, but there is no Enable-Migrations command (or DbMigrationsConfiguration classes) in Entity Framework Core.我不知道你哪里出错了,但 Entity Framework Core 中没有Enable-Migrations命令(或DbMigrationsConfiguration类)。 Those are only an Entity Framework 6 thing.这些只是实体框架 6 的事情。

Firstly lets get you on the right page...首先让你进入正确的页面......

https://docs.microsoft.com/en-us/ef/core/get-started/uwp/getting-started https://docs.microsoft.com/en-us/ef/core/get-started/uwp/getting-started

Second the unfortunate news... but there appears someone has adapted it to have a seeding feature.其次是不幸的消息……但似乎有人对其进行了改编,使其具有播种功能。 Right not is not currently supported in the latest release by MS, its a second party that produced the code.目前,MS 的最新版本不支持 Right not,它是生成代码的第二方。 It's an adaptation of the code that was previously in the EF6 release.它是对先前在 EF6 版本中的代码的改编。 Also keep in mind that code is based on Asp.net Core not UWP.另请记住,代码基于 Asp.net Core 而不是 UWP。

https://github.com/aspnet/EntityFramework/issues/629 https://github.com/aspnet/EntityFramework/issues/629

Old question, but I hope it can help.老问题,但我希望它可以帮助。

I had the same issue trying to use some EF6 code with a new EFCore project, I walked many tutorial to seed using the "Core" way, but I was not satisfied (I don't like to see seed data inside migrations...in my opinion the old school process, first update schema, then seed data, makes more sense).我在尝试在新的 EFCore 项目中使用一些 EF6 代码时遇到了同样的问题,我使用“Core”方式走了很多教程来播种,但我并不满意(我不喜欢在迁移中看到种子数据......在我看来,旧学校的过程,首先更新模式,然后种子数据,更有意义)。

By the way, I ended up with the most simple (but not obvious) solution: seed in Startup.cs > Configure method!顺便说一句,我最终得到了最简单(但不明显)的解决方案:Startup.cs 中的种子 > 配置方法!

  1. add your context as a param for the Configure method添加您的上下文作为配置方法的参数
  2. call your SeedData method调用您的 SeedData 方法
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ... , MyDBContext context)
{
   ...
   ContextInizializer.SeedData(context);
   ...
}
  1. Add/update data添加/更新数据
public class ContextInizializer
{
   public static void SeedData(RPToolDBContext context)
   {

      var item = new MyEntity()
      {
         Title = "MyTitle",
      };
      if(context.MyEntities.FirstOrDefault(f=>f.Title == item.Title)) {
         context.MyEntities.Add(item);
         context.SaveChanges();
      }
   }
}

Seed will run on every startup (in EF6 way was only after the update-database command), so be sure to check before adding stuff!种子将在每次启动时运行(在 EF6 方式中仅在 update-database 命令之后),所以在添加东西之前一定要检查!

Done!完毕!

Add using System.Data.Entity.Migrations;添加using System.Data.Entity.Migrations;

This would fix the issue for you.这将为您解决问题。
The error is because you have not referenced the library which contains definition for DbMigrationCongfiguration .错误是因为you have not referenced the library which contains definition for DbMigrationCongfiguration

Hope this helps.希望这可以帮助。

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

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