无法创建类型为’DataContext’的对象。

huangapple go评论62阅读模式
英文:

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&lt;User, ApplicationRole, Guid&gt;
{
    public DataContext(DbContextOptions&lt;DataContext&gt; opts) : base(opts) { }


    public DbSet&lt;User&gt; Users { get; set; }

    public DbSet&lt;Coach&gt; Coaches { get; set; }

    public DbSet&lt;Coaching&gt; Coachings { get; set; }

    public DbSet&lt;CoachingVideo&gt; CoachingVideos { get; set; }

    public DbSet&lt;AppFile&gt; 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&lt;DataContext&gt;(
                o =&gt; o.UseSqlServer(builder.GetConnectionString(&quot;DefaultConnection&quot;)));
        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&lt;DataContext&gt;(options =&gt;
            {
                options.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;));
            });

or

services.AddDbContext&lt;DataContext&gt;(options =&gt;
            {
                options.UseSqlServer(&quot;Conection string&quot;);
            });

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(&quot;CONNECTION STRING&quot;);
        }
    }

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(&quot;CONNECTION STRING&quot;);
        
    }

huangapple
  • 本文由 发表于 2023年6月29日 00:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定