How to avoid "null value in column violates not-null constraint" using `ValueGenerationOnAdd()`?

0

I followed https://stackoverflow.com/a/50203032 to use ValueGenerationOnAdd() to avoid Npgsql.PostgresException (0x80004005): 23502: null value in column "BlogId" violates not-null constraint. Here is my code:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
        optionsBuilder.UseNpgsql(@"Server=localhost;Port=5432;Database=Blogging;User Id=eth;Password=abc;");
    }


protected override void OnModelCreating(ModelBuilder modelBuilder){
    modelBuilder.Entity<Blog>()
    .Property(p => p.BlogId)
    .ValueGeneratedOnAdd();
}

}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; } = new List<Post>();
}
public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

But Still have the same error. Did I miss something?

$ dotnet run
Inserting a new blog
Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
 ---> Npgsql.PostgresException (0x80004005): 23502: null value in column "BlogId" violates not-null constraint
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
  Exception data:
    Severity: ERROR
    SqlState: 23502
    MessageText: null value in column "BlogId" violates not-null constraint
    Detail: Failing row contains (null, http://blogs.msdn.com/adonet).
    SchemaName: public
    TableName: Blogs
    ColumnName: BlogId
    File: execMain.c
    Line: 2017
    Routine: ExecConstraints
postgresql
entity-framework
npgsql
asked on Stack Overflow Jan 9, 2020 by Ethan • edited Jan 10, 2020 by Ethan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0