gpt4 book ai didi

c# - DbContext 必须可转换为 System.Data.Entity.DbContext

转载 作者:行者123 更新时间:2023-11-30 20:00:22 28 4
gpt4 key购买 nike

在我的应用程序中,我刚刚将 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.

错误是因为我更新了我的 Entity Framework 并且新的 Entity Framework 中发生了一些变化吗?还是我做错了什么?未显示错误的 Entity Framework 版本为 6.1.0-alpha1

最佳答案

同样的事情发生在我身上,在我的例子中,是 ReSharper-cache 试图让我感到困惑。

我是这样修复的:

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

关于c# - DbContext 必须可转换为 System.Data.Entity.DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21277509/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com