英文:
Unable to create an object of type 'DataContext'
问题
我已经像我一直做的那样创建了默认的数据库上下文,并尝试添加迁移。我遇到了一个错误:
> "无法创建类型为 'DataContext' 的对象。有关在设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728"
我的数据库上下文:
public class DataContext : IdentityDbContext<User, ApplicationRole, Guid>
{
public DataContext(DbContextOptions<DataContext> opts) : base(opts) { }
public DbSet<User> Users { get; set; }
public DbSet<Coach> Coaches { get; set; }
public DbSet<Coaching> Coachings { get; set; }
public DbSet<CoachingVideo> CoachingVideos { get; set; }
public DbSet<AppFile> Files { get; set; }
}
我的数据库上下文注入(已经在 Program.cs 中):
public static class DataContextExtension
{
public static IServiceCollection AddDataContext(this IServiceCollection services, IConfiguration builder)
{
services.AddDbContext<DataContext>(
o => o.UseSqlServer(builder.GetConnectionString("DefaultConnection")));
return services;
}
}
我尝试安装不同的版本,但没有任何改变...
英文:
I've created default DB context as I do always and tried to add migration. I got an error:
> "Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728"
My DB Context:
public class DataContext : IdentityDbContext<User, ApplicationRole, Guid>
{
public DataContext(DbContextOptions<DataContext> opts) : base(opts) { }
public DbSet<User> Users { get; set; }
public DbSet<Coach> Coaches { get; set; }
public DbSet<Coaching> Coachings { get; set; }
public DbSet<CoachingVideo> CoachingVideos { get; set; }
public DbSet<AppFile> Files { get; set; }
}
My DB Context injection (already in Program.cs)
public static class DataContextExtension
{
public static IServiceCollection AddDataContext(this IServiceCollection services, IConfiguration builder)
{
services.AddDbContext<DataContext>(
o => o.UseSqlServer(builder.GetConnectionString("DefaultConnection")));
return services;
}
}
I tried to install different versions, but nothing...
答案1
得分: 1
首先,请确保连接字符串正确
然后使用以下代码之一:
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
或
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer("Conection string");
});
如果上述代码不起作用,请使用以下代码部分 1
public DataContext(DbContextOptions options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlServer("CONNECTION STRING");
}
}
如果上述代码仍然不起作用,请暂时禁用您的代码,并输入以下代码,然后在更新数据库后,将此代码注释掉并激活您的代码。
//public DataContext(DbContextOptions options) : base(options)
// {
// }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("CONNECTION STRING");
}
英文:
First, make sure the connection string is correct
Then use this code
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
or
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer("Conection string");
});
If the above code does not work , use this code section 1
public DataContext(DbContextOptions options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlServer("CONNECTION STRING");
}
}
If the above code does not work, disable your code temporarily and enter this code and after updating the database, comment this code and activate your code.
//public DataContext(DbContextOptions options) : base(options)
// {
// }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("CONNECTION STRING");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论