Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbContext must be convertible to System.Data.Entity.DbContext

In my application I just updated EntityFramework to version 6.0.2 and now i'm getting some error which I didn't before I updated my application.

I had a DbContext file that inherited from IdentityDbContext class. For example

public class DatePickerDbContext : IdentityDbContext<ApplicationUser>
{
    public DatePickerDbContext():base("DefaultConnection")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("Users");
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }
    public DbSet<License> Licenses { get; set; }
    public DbSet<AddressBook> AddressBooks { get; set; }
    public DbSet<AddressBookPerson> AddressBookPersons { get; set; }

    public DbSet<Appointment> Appointments { get; set; }

    public DbSet<Attendee> Attendees { get; set; }

    public DbSet<Response> Responses { get; set; } 

}

before I updated my application the following line was completely fine

 Database.SetInitializer<DatePickerDbContext>(new DatePickerDbInitializer());

datepickerdbinitializer class:

 public class DatePickerDbInitializer:CreateDatabaseIfNotExists<DatePickerDbContext>
    {
        protected override void Seed(DatePickerDbContext context)
        {
            InitializeDatePickerDbForEf(context);
            base.Seed(context);
        }

        private static void InitializeDatePickerDbForEf(DatePickerDbContext context)
        {
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
            var licenseTrial = new License
            {
                Id = 1,
                Name = "Trial",
                Description = "Works for 14 days"
            };

            var licenseFull = new License
            {
                Id = 2,
                Name = "Full",
                Description = "Subscription based"
            };
            context.Licenses.Add(licenseTrial);
            context.Licenses.Add(licenseFull);

            var user = new ApplicationUser
            {
                UserId = new Guid(),
                UserName = "biplov",
                IsApproved = true,
                License = licenseFull,
                DateTimeRegistered = DateTime.UtcNow
            };

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new IdentityRole("Admin"));
            }

            var createUser = userManager.Create(user,"biplov");
            if (createUser.Succeeded)
            {
                userManager.AddToRole(user.Id,"Admin");
            }

        }
    }

but now I get a complain saying `The type DatePicker.Models.DatePickerDbContext must be convertible to System.Data.Entity.DbContext in order to use it as parameter 'TContext' in the generic method 'void System.Data.Entity.Database.SetInitializer<TContext>(IDatabaseInitialize<TContext>)'`
![enter image description here][1]

I still have another application in MVC5 from web called AspnetIdentitySample whose DbContext also inherits from IdentityDbContext<TUser> class but has no prob in global.asax's `Database.SetInitializer` method

    public class MyDbContext : IdentityDbContext<ApplicationUser>
    {
        public MyDbContext()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Change the name of the table to be Users instead of AspNetUsers
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users");
            modelBuilder.Entity<ApplicationUser>()
                .ToTable("Users");
        }

        public DbSet<ToDo> ToDoes { get; set; }

        public DbSet<MyUserInfo> MyUserInfo { get; set; }
    }

in global.asax

Database.SetInitializer<MyDbContext>(new MyDbInitializer());//no error.

Is the error because I updated my entity framework and something has changed in new entity framework? or have I done something wrong? the version of entity framework where error is not being shown is 6.1.0-alpha1

like image 931
Cybercop Avatar asked Aug 17 '26 03:08

Cybercop


2 Answers

Same thing just happened to me, in my case it was ReSharper-cache trying to confuse me.

This is how I fixed it:

Tools -> Options -> ReSharper -> Options -> General -> Clear Caches
like image 52
Mirage Avatar answered Aug 18 '26 16:08

Mirage


May be it is a problem from Visual studio. I closed visual studio, opened it again, cleaned the solution, builded it, then run it. Now the program runs fine, but it still complains in that part for cannot be converted. Just ignore it.

like image 39
Cybercop Avatar answered Aug 18 '26 18:08

Cybercop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!