2
votes

I have added Microsoft.AspNetCore.Identity.EntityFrameworkCore via nuget.

StartUp

public void ConfigureServices(IServiceCollection services)
{
    //Add PostgreSQL support
    services.AddEntityFrameworkNpgsql()
        .AddDbContext<StoreDbContext>(options =>
            options.UseNpgsql(Configuration["Data:StoreDbContext:ConnectionString"]));
    // Add framework services.
    services.AddMvc();

    services.AddIdentity<PostgreApp.Models.User, IdentityRole>()
            .AddEntityFrameworkStores<StoreDbContext>()
            .AddDefaultTokenProviders();

    // Add our PostgreSQL Repository
    services.AddTransient<IStoreRepository, StoreRepository>();
}

In StartUp' Configure I have added following:

app.UseIdentity();

This is my code:

 public class StoreDbContext: IdentityDbContext<User>
 {
    public StoreDbContext(DbContextOptions<StoreDbContext> options) : base(options)
    {
    }

    public DbSet<Store> Stores { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<JobPost> JobPosts { get; set; }
    public DbSet<Candidate> Candidates { get; set; }
    public DbSet<CandidateCV> CandidateCVs { get; set; }
    public DbSet<Criterion> Criteria { get; set; }
    public DbSet<AssessmentStage> AssessmentStages { get; set; }
    public DbSet<AssessmentCriterion> AssessmentCriteria { get; set; }
    public DbSet<JobpostCv> JobpostCvs { get; set; }
}

Then I am getting this:

System.InvalidOperationException: Column Id of type text has ValueGenerated.OnAdd but no default value is defined at Microsoft.EntityFrameworkCore.Migrations.NpgsqlMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable1 unicode, Nullable1 maxLength, Boolean rowVersion, Boolean nullable, Object default Value, String defaultValueSql, String computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(CreateTableOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(CreateTableOperation operation, IModel model, MigrationCommandListBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder) at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.b__0()

at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[]

args) at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) Column Id of type text has ValueGenerated.OnAdd but no default value is defined

How do I solve this ?

2

2 Answers

0
votes
services.AddIdentity<StoreDbContext, IdentityRole>()
    .AddDefaultTokenProviders();

Instead of StoreDbContext you should be providing User(ApplicationUser) class.

What is the version of ASP.NET Core / EF Core that you are using?

0
votes

Can you check the User objects implementation that it inherits the ApplicationUser properly. It seems like you have an entity which doesn't have the auto genaration policy properly implemented.

Can you check the applicationUser class in the migration file about the the way it's generating the user id property, as it is a GUID so it might need to handled. You might have something like

 modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b =>
                {
                    b.Property<string>("Id")
                        .ValueGeneratedOnAdd();

Where you are tryin to apply Autogenation on a string field.

and in your migration files you should have Annotation("Npgsql:ValueGeneratedOnAdd", true); associated with string values.

You can try implementitn the IdentityUser instead of string

  public class ApplicationUser : IdentityUser<Guid>
    {
    }

Refer to the following link https://github.com/aspnet/Docs/tree/master/aspnetcore/security/authentication/identity/sample/src