Entity Framework Core Enable-Migrations build the Configuration.cs file with Errors
Asked Answered
C

4

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" }
        //    );
        //
    }
}
Cirone answered 17/2, 2017 at 5:26 Comment(6)
what is the error when you compileWinze
Intellisence would answer this betterLeuctra
If I am right, you are talking about the underlined errors. Right?Leuctra
can you tell how you enable migrations?Winze
if you want to add seed please read this post learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/introWinze
@Cirone is the issue fixed?Nonparticipation
P
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.

Pantaloons answered 21/2, 2017 at 16:47 Comment(1)
Yes. And this is a little sad.Philologian
E
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!

Ekaterina answered 28/3, 2020 at 22:36 Comment(0)
C
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

Crustal answered 18/2, 2017 at 21:9 Comment(1)
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?Cirone
L
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.

Leuctra answered 17/2, 2017 at 5:41 Comment(8)
yes if you see that its reference Microsoft.EntityFrameworkCore not System.Data.EntityWinze
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 for reference.Leuctra
its for Entity Framework 6 not Entity Framework Core please readWinze
@Winze Seed is a function in DbMigrationsConfiguration class which is found in the System.Data.Entity.Migrations namespaceNonparticipation
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.DataCirone
Intelisense only shows System.Data.Common as your typing.. No other value exist besides commonCirone
@Cirone 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.Nonparticipation
3 years later, this question is still valid, but this answer is really confusing.Carmacarmack

© 2022 - 2024 — McMap. All rights reserved.