Pouriya
Pouriya

Reputation: 43

I can not add migration when I want add Identity to my project in ASP.NET Core

I want to use identity in my project. I did create identity DbContext and also I did add Identity services to my startup file

But when I want create a new migration I see this error:

Unable to create an object of type context name for the deferent design patterns supported at design time

My context code here:

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

        }
}

My startup codes here

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

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

Please helps me I did search in google but I didn't find any results

Upvotes: 1

Views: 627

Answers (2)

Tupac
Tupac

Reputation: 2910

I Resolved this by just adding a plain constructor to my Context

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);            
    }
}

Upvotes: 0

Pouriya
Pouriya

Reputation: 43

I solved the problem

I did add seed data in my context but I did not pass this to identity dbcontext and now I did pass this with :

base.OnModelCreating(modelBuilder);

Upvotes: 1

Related Questions