5

I ran Enable-Migrations after much drama I finally got the command right so that it creates the Migrations directory within my project.

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.

Configuration with errors

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" }
        //    );
        //
    }
}
Smit
  • 2,279
  • 1
  • 12
  • 22
ekgcorp
  • 165
  • 1
  • 11

4 Answers4

11

I have no idea where you went wrong, but there is no Enable-Migrations command (or DbMigrationsConfiguration classes) in Entity Framework Core. Those are only an Entity Framework 6 thing.

bricelam
  • 28,825
  • 9
  • 92
  • 117
2

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).

By the way, I ended up with the most simple (but not obvious) solution: seed in Startup.cs > Configure method!

  1. add your context as a param for the Configure method
  2. call your SeedData method
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!

Done!

1

Firstly lets get you on the right page...

https://learn.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. It's an adaptation of the code that was previously in the EF6 release. Also keep in mind that code is based on Asp.net Core not UWP.

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

mvermef
  • 3,814
  • 1
  • 23
  • 36
  • That was the tutorial that I followed, and when I finally was able to "Enable-Migration" this is the file that was created above. So what your saying is, the people that created this code for EFCore, did not make it aware of UWP? – ekgcorp Feb 20 '17 at 16:16
0

Add 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.

Hope this helps.

Prashanth Benny
  • 1,523
  • 21
  • 33
kaido
  • 314
  • 1
  • 13
  • yes if you see that its reference `Microsoft.EntityFrameworkCore` not `System.Data.Entity` – Usman Feb 17 '17 at 06:26
  • He's already using EntityFrameworkCore. Which is unbroken. The namespace for DbMigrationsConfiguration class is in the System.Data.Entity.Migrations. please refer this [msdn link](https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration(v=vs.113).aspx) for reference. – kaido Feb 17 '17 at 06:36
  • 8
    its for Entity Framework 6 not Entity Framework Core please read – Usman Feb 17 '17 at 07:11
  • @Usman `Seed` is a function in `DbMigrationsConfiguration` class which is found in the `System.Data.Entity.Migrations` namespace – Prashanth Benny Feb 17 '17 at 10:44
  • 1
    When I originally ran Enable-Migration successful. Enable-Migrations -StartUpProjectName RapidDeploy -ContextTypeName RapidDeploy.Models.BloggingContext -Verbose..... It created both using statements System.Data.Entity and System.Data.Entity.Migrtations.. However both of them had an error on Entity, saying that it does not exist in the Name Space System.Data – ekgcorp Feb 17 '17 at 16:02
  • Intelisense only shows System.Data.Common as your typing.. No other value exist besides common – ekgcorp Feb 17 '17 at 16:06
  • @ekgcorp probably you need to install the latest entity framework nuget package(or upgrade to latest). Or check the references in the project, find entity framework, if it's missing, you need to install it else upgrade. – Prashanth Benny Feb 18 '17 at 05:58
  • 2
    3 years later, this question is still valid, but this answer is really confusing. – NotImplementedException Feb 11 '20 at 15:23