2014-01-22 91 views
2

在我的應用程序中,我剛更新了EntityFramework到版本6.0.2,現在我收到了一些錯誤,我沒有更新我的應用程序之前。DbContext必須轉換爲System.Data.Entity.DbContext

我有一個繼承自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

+0

所以跟隨領隊:查找IdentityDbContext(使用F12)。請注意名稱空間和基類。 –

+0

看起來像一個Resharper或Visual Studio視覺錯誤:我有一個錯誤,說IdentityUser無法轉換爲字符串表示,但構建解決方案工作得很好。一個Resharper緩存清理和VS重新啓動修復它(雖然我不知道緩存清理是否有所不同)。 –

回答

3

可能是Visual Studio的問題。 我關閉了visual studio,再次打開它,清理解決方案,建立它,然後運行它。現在程序運行良好,但仍然抱怨該部分無法轉換。只要忽略它。

+0

您是否安裝了Resharper? – joelmdev

4

同樣的事情發生在我身上,在我的情況下,它是ReSharper緩存試圖混淆我。

這是我如何固定它:

Tools -> Options -> ReSharper -> Options -> General -> Clear Caches 
相關問題