PrimaryKey increment on Insert?
Posted: Fri 27 Feb 2009 23:35
Using:
Visual Studio 2008 9.0.30729.1 SP
dotConnect for PostgreSQL 4.0.22.0 (eval version)
.NET 3.5 SP1
Windows XP, SP3
Here is some representative C# Linq code that's been working to date using Microsoft SQL Linq classes. For a table named "Foos" with just two columns "Id" and "Name", where Id is the primary key:
Foo foo = Foos.FirstOrDefault(f => f.Name == name);
if (foo == null)
{
foo = new Foo() { Name = name };
Foos.InsertOnSubmit(foo);
SubmitChanges();
}
Note that this code initializes the Name column but not the Id column. MS LinqToSql classes and SQL Server would assign a new unique value for the Id. This behavior doesn't seem to be happening when using PostgreSQL and Devart's corresponding Linq classes. The first insert succeeds, providing a default value of 0 for the foo's Id, but then any subsequent inserts fail (because the code attempts to use a default Id of 0 again, apparently).
In case it's relevant, here is the corresponding SQL Table. (It was generated by a tool I used to migrate the original MS SQL database to PostgreSQL):
CREATE TABLE "Foos"
(
"Id" bigserial NOT NULL,
"Name" character(64) NOT NULL DEFAULT ''::bpchar,
CONSTRAINT "Foos_pkey" PRIMARY KEY ("Id")
)
WITH (OIDS=FALSE);
ALTER TABLE "Foos" ADD COLUMN "Id" bigint;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET STORAGE PLAIN;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET NOT NULL;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET DEFAULT nextval('"Foos_Id_seq"'::regclass);
Do the Devart-generated classes really fail to invoke nextval on insertion? Do they provide some other primary-key/row-insert support for us programmers to use for inserting rows into the database? Do the generated classes only do the primary key assignment if the PostgreSQL primary key is set up a particular way?
Regards,
D.Clark, Software Engineer
Visual Studio 2008 9.0.30729.1 SP
dotConnect for PostgreSQL 4.0.22.0 (eval version)
.NET 3.5 SP1
Windows XP, SP3
Here is some representative C# Linq code that's been working to date using Microsoft SQL Linq classes. For a table named "Foos" with just two columns "Id" and "Name", where Id is the primary key:
Foo foo = Foos.FirstOrDefault(f => f.Name == name);
if (foo == null)
{
foo = new Foo() { Name = name };
Foos.InsertOnSubmit(foo);
SubmitChanges();
}
Note that this code initializes the Name column but not the Id column. MS LinqToSql classes and SQL Server would assign a new unique value for the Id. This behavior doesn't seem to be happening when using PostgreSQL and Devart's corresponding Linq classes. The first insert succeeds, providing a default value of 0 for the foo's Id, but then any subsequent inserts fail (because the code attempts to use a default Id of 0 again, apparently).
In case it's relevant, here is the corresponding SQL Table. (It was generated by a tool I used to migrate the original MS SQL database to PostgreSQL):
CREATE TABLE "Foos"
(
"Id" bigserial NOT NULL,
"Name" character(64) NOT NULL DEFAULT ''::bpchar,
CONSTRAINT "Foos_pkey" PRIMARY KEY ("Id")
)
WITH (OIDS=FALSE);
ALTER TABLE "Foos" ADD COLUMN "Id" bigint;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET STORAGE PLAIN;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET NOT NULL;
ALTER TABLE "Foos" ALTER COLUMN "Id" SET DEFAULT nextval('"Foos_Id_seq"'::regclass);
Do the Devart-generated classes really fail to invoke nextval on insertion? Do they provide some other primary-key/row-insert support for us programmers to use for inserting rows into the database? Do the generated classes only do the primary key assignment if the PostgreSQL primary key is set up a particular way?
Regards,
D.Clark, Software Engineer