【问题标题】:I can not add migration when I want add Identity to my project in ASP.NET Core当我想在 ASP.NET Core 中将身份添加到我的项目时,我无法添加迁移
【发布时间】:2021-09-07 04:47:25
【问题描述】:

我想在我的项目中使用身份。我确实创建了身份DbContext,并且我确实将身份服务添加到了我的启动文件中

但是当我想创建一个新的迁移时,我看到了这个错误:

无法为设计时支持的不同设计模式创建上下文名称类型的对象

我的上下文代码在这里:

public class websitecontext : IdentityDbContext
{
    public websitecontext(DbContextOptions<websitecontext> options) : base(options)
        {

        }
}

我的启动代码在这里

services.AddDbContext<websitecontext>(s =>
s.UseSqlServer(Configuration.GetConnectionString("websiteconnectionstring"))
            );

            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<websitecontext>()
                .AddDefaultTokenProviders();

请帮助我,我在 google 中搜索过,但没有找到任何结果

【问题讨论】:

  • 您能否显示您的注册身份代码以及您如何定义您的应用程序 dbContext ?
  • 在这里分享你的相关代码。
  • 让我编辑我的问题...
  • 我在问题中添加了上下文代码
  • 您使用软件包控制台还是普通 CLI?任何一个人都必须知道您的 Startup 项目。

标签: c# .net asp.net-core .net-core


【解决方案1】:

我解决了问题

我确实在我的上下文中添加了种子数据,但我没有将其传递给身份 dbcontext,现在我确实通过了:

base.OnModelCreating(modelBuilder);

【讨论】:

    【解决方案2】:

    我通过在我的上下文中添加一个普通的构造函数解决了这个问题

    public class DataContext : DbContext
    {
        public DataContext()
        {
        }
    
        public DataContext(DbContextOptions options) : base(options)
        {
        }
    
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            if (!options.IsConfigured)
            {
                options.UseSqlServer("A FALLBACK CONNECTION STRING");
            }
        }
        
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2019-09-30
      • 1970-01-01
      相关资源
      最近更新 更多