DbContext必须可转换为System.Data.Entity.DbContext

时间:2014-01-22 08:14:22

标签: c# .net asp.net-mvc entity-framework

在我的应用程序中,我刚刚将EntityFramework更新到版本6.0.2,现在我收到了一些错误,我在更新应用程序之前没有这样做。

我有一个继承自IdentityDbContext类的DbContext文件。例如

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; } 

}

在我更新我的应用程序之前,以下行完全正常

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

datepickerdbinitializer类:

 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; }
    }
global.asax

中的

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

错误是因为我更新了我的实体框架并且新实体框架中的内容发生了变化吗?或者我做错了什么? 未显示错误的实体框架版本是6.1.0-alpha1

2 个答案:

答案 0 :(得分:4)

同样的事情发生在我身上,在我的情况下,ReSharper-cache试图让我迷惑。

这就是我修复它的方法:

Tools -> Options -> ReSharper -> Options -> General -> Clear Caches

答案 1 :(得分:3)

可能是Visual Studio的一个问题。 我关闭了visual studio,再次打开它,清理解决方案,构建它,然后运行它。现在该程序运行良好,但它仍然抱怨该部分无法转换。只是忽略它。