1
votes

I've got a problem when i try to add Record to a Table which contains only one column MandantId as integer primary key. This column is not auto-increment. When I try to add a Record I get the following error message: "23502: null value in column "mandantid" violates not-null constraint"

What i use:

  1. PostgreSQL 9.6
  2. EntityFramework 6.1.3
  3. EntityFramework6.Npgsql 3.1.1
  4. Npgsql 3.1.2

If I use a INSERT query like: _context.Database.ExecuteSqlCommand(string.Format("INSERT INTO public.mandant (mandantid) VALUES ({0});"), MandantId)); It works!

But I don't want to use a fix query. What I've already tried:

  1. With the Add() and SaveChanges() functions from EF, once with 0 and once valid integer, same error
  2. Removed all dependencies on Database and EF, same error
  3. Using an insert script, which worked
  4. Added an additional column named id as primary key as auto-increment, which worked. But then i have two unique keys.

This is my Databasemodel:

[Table("mandant", Schema = "public")]
public class MandantEdm
{
    public MandantEdm()
    {
    }

    [Key]
    [Column("mandantid")]
    public int MandantId { get; set; }
}

This is the context-class:

class LicenseContext : DbContext
{
    public LicenseContext()
        : base("MandantContext")
    {
    }

    public virtual DbSet<MandantEdm> Mandants { get; set; }
}
1
I have this problem too. Are you resolve it?kazakov.nickolay

1 Answers

0
votes

I've got this problem too. And resolved it. You should add Order value to Column attribute and DatabaseGenerated attribute for your primary key.

[Table("mandant", Schema = "public")]
public class MandantEdm
{
    public MandantEdm()
    {
    }

    [Key]
    [Column("mandantid", Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int MandantId { get; set; }
}