column \“Id \”违反非空约束“

时间:2016-12-05 18:24:03

标签: c# postgresql

我今天刚开始使用PostgreSQL并在我的演示数据库中创建了一个Person表,并尝试使用C#连接到数据库。数据库连接成功,但我面临Id列的空值错误,即使我清楚地传递它。这是我的代码:

  class Program
{
    static void Main(string[] args)
    {
        var db = new DB();

        var person = new Person { Id = 1, Name = "Stevie" };
        db.Persons.Add(person);
        db.SaveChanges();

        var persons = db.Persons;
        foreach (var p in persons)
        {
            Console.WriteLine(p.Name);
        }
        Console.Read();
    }
}

public class DB : DbContext
{
    public DB() : base(nameOrConnectionString: "Test") { }
    public DbSet<Person> Persons { get; set; }
}

[Table("Person", Schema = "public")]
public class Person
{
    [Key]
    [Column("Id")]
    public int Id { get; set; }
    [Column("Name")]
    public string Name { get; set; }
}

人员表SQL

CREATE TABLE public."Person"
 (
"Id" integer NOT NULL,
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "Person_pkey" PRIMARY KEY ("Id")
 );

0 个答案:

没有答案