简体   繁体   中英

DataAdapter / CommandBuilder - Unique constraint on multiple columns

Im using command builder to auto generate SQL statements and DataAdapter to fill DataSet Generally works well but now I have issue with Unique constraint on multiple columns

I have in DB (PostgreSQL) such unique index

CREATE UNIQUE INDEX idx_uniq ON table_1 (id,currency_id); 

DataAdapter / CommandBuilder read this wrongly and set unique flag on each column separately (id & currency_id), instead use combination of both as unique.

I have huge impact on my app now - because I cant even fill dataset - because it says constrains are broken. Any way to force DA to read that correctly or remove those wrong constrains ?

I stuck with that since few hrs thus any help / tips will be highly appreciated ...

If this is a flaw in the command builder, your quickest resolution may be to remove the existing constraint and manually add a new one:

UniqueConstraint unique = 
   new UniqueConstraint(new { table_1.Columns["id"], 
                              table_1.Columns["currency_id"] });

// remove all unique constraints except primary key
// use ToList so that we can remove the constraint within the loop
for(c in table_1.Constraints
                .OfType<UniqueConstraint>()
                .Where(uc => !uc.IsPrimaryKey)
                .ToList())
{
    table_1.Constraints.Remove(c);
}
table_1.Constraints.Add(unique );

or just turn off constraints altogether:

dataset.EnforceConstraints = false;

but this may disable some constraints that you actually want (like foreign key constraints).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM