我今天刚开始使用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")
);