开发者

How to get auto increment identity column working

We a开发者_如何学Gore using EF Core, but we also upgrade pre-ef core databases to the latest version. On Postegres there is a table:

CREATE TABLE "DatabaseVersion"  
( 
    "DatabaseVersionId" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ) PRIMARY KEY, 
    "Version"     varchar(20) NOT NULL,  
    "VersionDate" timestamptz NULL DEFAULT Now(), 
    "Description" varchar(2000) NULL, 
    "AppliedDate" timestamptz NULL DEFAULT Now()
);

In PgAdmin, I create a database and this table, then I insert into the table for a test using:

INSERT INTO "DatabaseVersion" ("Version") 
VALUES ('1.0.0.0');

and I get the first row entered then I can change the version in the insert statement and insert more rows. Each row has a unique DatabaseVersionId.

However, when I run the upgrade and attempt to insert a value using EF Core, I get an exception

duplicate key value violates unique constraint

I created a new database to see how EF Core defined the key and it is:

GENERATED BY DEFAULT AS IDENTITY (INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1).

I added primary key to that to avoid adding a lot of primary key constraints to our older scripts.

Our update code is shown here (T would be DatabaseVersion):

var dbSet = context.Set<T> ();

var id = GetKey (context, item);

if (id == 0)
{
    dbSet.Add (item);
}
else
{
    var p = dbSet.Find (id);
    dbSet.Attach (p);

    var a = context.Entry (p);

    if (a != null)
        a.CurrentValues.SetValues (item);
}

context.SaveChanges ();

What needs to be done to older scripts to make the identity primary key work?

Or is that even the problem? I am not a postgres expert.

Our database upgrade uses scripts until it gets to a certain version, then we run each migration individually. EF Core figures out not to run the initial create migration on its own, but it still adds an entry in the __EFMigrationsHistory table, which is great.


The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Person id" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜