0

I am trying to enable migrations for data entity: In Package Manager Console:

Enable-Migrations -ProjectName Vidly -ContextTypeName Vidly.Models.MyDBContext

I get:

Code First Migrations enabled for project Vidly.

Then

add-migration 'InitialModel'

I get: "The project 'Vidly' failed to build."

My Configuration.cs:

namespace Vidly.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

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

        protected override void Seed(Vidly.Models.MyDBContext 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.
        }
    }
}

And my MyDBContext.cs is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models
{
    public class MyDBContext
    {
        public DbSet<Customer> Customers { get; set; } // My domain models
        public DbSet<Movie> Movies { get; set; }// My domain models
    }
}

The error message is:

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

It seems I cannot use MyDBContext as a type super class in Configuration.cs. Please help. Thanks.

3 Answers 3

2

You are missing to implement DbContext.

Change AND try:

public class MyDBContext:DBContext
    {
        public  MyDBContext() : base("ConnectionStringName")
        {           
        }
        public DbSet<Customer> Customers { get; set; } // My domain models
        public DbSet<Movie> Movies { get; set; }// My domain models
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Couple of things you are missing here.

  1. Inheriting from DBContext
  2. Constructor to pass the name of connection string entry you used in

use below snippet of code. note: replace VidlyDBConnectionString with your connection string name

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    namespace Vidly.Models
    {
        public class MyDBContext :DbContext
         {

            public MyDBContext():base("name=VidlyDBConnectionString")
            {
             }
            public DbSet<Customer> Customers { get; set; } // My domain models
            public DbSet<Movie> Movies { get; set; }// My domain models


        }
    }

Comments

0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models;
   {
        public class MyDBContext :DbContext
         {
            public DbSet<Customer> Customers { get; set; } // My domain models
            public DbSet<Movie> Movies { get; set; } // My domain models
         }

   }

1 Comment

This should solve your problem. Let us know is it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.