在我的應用程序中,我剛更新了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
所以跟隨領隊:查找IdentityDbContext(使用F12)。請注意名稱空間和基類。 –
看起來像一個Resharper或Visual Studio視覺錯誤:我有一個錯誤,說IdentityUser無法轉換爲字符串表示,但構建解決方案工作得很好。一個Resharper緩存清理和VS重新啓動修復它(雖然我不知道緩存清理是否有所不同)。 –