0

I am working with .net MVC. I have enabled migrations in my project which has successfully been done. But I'm getting an error in Migrations>Configrations.cs. The error is following.

CS0311: The type 'BabyStore.DAL.StoreContext' cannot be used as type parameter 'TContext' in the generic type or method 'DbMigrationsConfiguration'. There is no implicit reference conversion from 'BabyStore.DAL.StoreContext' to 'System.Data.Entity.DbContext'

I have enabled migration as following

PM>Enable-Migrations -ContextTypeName BabyStore.DAL.StoreContext

The code of BabyStore.DAL.ContextStore.cs is

    namespace BabyStore.DAL
{
    public class StoreContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }

    }
}

And lastly The code where I'm getting error is following BabyStore.Migrations.Configurations.cs

   namespace BabyStore.Migrations
{

using System.Data.Entity.Migrations;

    internal sealed class Configuration : DbMigrationsConfiguration<BabyStore.DAL.StoreContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(BabyStore.DAL.StoreContext _context)
        {
            var categories = new List<Category>
            {
            new Category { Name = "Clothes"},
            new Category { Name = "Play and Toy" }
            };
            categories.ForEach(c => _context.Categories.AddOrUpdate(p => p.Name, c));
            _context.SaveChanges();

            var products = new List<Product>
            {
            new Product {Name="Sleep Suit", Desccription="For Sleeping wear",Price=100,CategoryID=categories.Single(c=>c.Name=="Clouths").ID },
            new Product {Name="Vest", Desccription="For Sleeping wear",Price=200,CategoryID=categories.Single(c=>c.Name=="Clouths").ID}
             };
            products.ForEach(c => _context.Products.AddOrUpdate(p => p.Name, c));
            _context.SaveChange();

        }
    }
}

I'm getting error in internal sealed class Configuration : DbMigrationsConfiguration line

Najeeb KHAN
  • 41
  • 10
  • Can check this out? "https://stackoverflow.com/questions/13980335/entity-framework-code-first-configuration-cs-seed-or-custom-initializer" or this "https://stackoverflow.com/questions/52604618/there-is-no-implicit-reference-conversion-from-applicationdbcontext-to-microsoft". More like you are referencing to wrong ef package. In Nuget Package Manager, find "Microsoft.EntityFrameworkCore" and install it – phonemyatt Dec 06 '19 at 08:01
  • i suspect you are using Entity Framework 6 and running the command from Entity Framework Core – phonemyatt Dec 06 '19 at 08:06
  • I don't have any knowledge regarding to .not Core. I'm learning .net MVC from a book (ASP.NET MVS with EntityFramwork and CSS by Lee Naylor). I'm just following the steps as mentioned in the book. But i'm stuck in the error i have mentioned above. – Najeeb KHAN Dec 07 '19 at 06:39
  • And Yes, the link you have shared with me, is a good resource. I have followed the instructions was given in the link and it works fortunately. But my question is still that why it is giving an error in case if I follow every step that is given in the book? – Najeeb KHAN Dec 07 '19 at 06:42
  • I haven't used EF in a while but doesn't your `StoreContext` need to inherit from `DbContext`? That error message implies `TContext` is constrained to a `System.Data.Entity.DbContext` – Chris Dec 07 '19 at 07:58

2 Answers2

1

How about force the migration? use -force parameter

PM>Enable-Migrations -ContextTypeName BabyStore.DAL.StoreContext -force

I think there is spelling mistake. Change clouths to cloths

new Product {Name="Sleep Suit", Desccription="For Sleeping wear",Price=100,CategoryID=categories.Single(c=>c.Name=="Cloths").ID },
new Product {Name="Vest", Description="For Sleeping wear",Price=200,CategoryID=categories.Single(c=>c.Name=="Cloths").ID}

I already check the book you are refereing to which is ASP.net MVC with Entity Framework CSS and it's repo https://github.com/Apress/asp.net-mvc-w-entity-framework-css. If you check the package, it use "EntityFramework.6.1.3" which is different from .net core.

You can recreate your project in visual studio and this time round select ASP.NET Web Application(.NET Framework). The problem is due to the different entity framework package. Please refer to this EntityFramework Core automatic migrations for setting up automatic migration on asp.net core.

phonemyatt
  • 1,287
  • 17
  • 35
  • I'm afraid it is still giving an error on the same line internal sealed class Configuration : DbMigrationsConfiguration It gives error on Configuration keyword – Najeeb KHAN Dec 07 '19 at 06:35
  • THANKS A LOT! I got the answer above. And yes I did try what you have recommended me to do so. Both of them worked for me. Thanks – Najeeb KHAN Dec 08 '19 at 18:27
0

Looking at the class signature shows that the generic argument TContext is constrained to type DbContext.

public class DbMigrationsConfiguration<TContext> : System.Data.Entity.Migrations.DbMigrationsConfiguration where TContext : DbContext

Modify your StoreContext to inherit System.Data.Entity.DbContext.

public class StoreContext : DbContext
Chris
  • 4,393
  • 1
  • 27
  • 33