c# - Why is the unique constraint missing when filling the datatable? -
i have following table in sql server
projects id(pk, int, not null) name (varchar(255), not null) public_key_token (varchar(50), null) i have added unique constraint name column using
alter table dbo.projects add constraint name_unique unique (name); which results in unique, non-clustered index on table (trusting ssms).
in code i'm retrieving table data using
using (sqldataadapter adapter = new sqldataadapter("select * " + dbtabname, con)) { using (datatable table = new datatable(dbtabname)) { datatable dt = adapter.fillschema(table, schematype.source); pkcolumns = dt.primarykey.select(c => c.columnname).tolist(); autoincrementcolumns = dt.primarykey.where(c => c.autoincrement).select(c => c.columnname).tolist(); uniquecolumns = dt.columns.cast<datacolumn>().where(c => c.unique).select(c => c.columnname).tolist(); ... } } the pks , autoincrement columns ok in uniquecolumns pk column again. name column arrives in c# without unique constraint.
changing schematype mapped did not alter result.
why lose constraint on way c#? missing on sql server side or in c#?
[update] @tim-schmelter's answer solves problem half way.
just adding index did not work. adding index when pk on id column exists doesn't work.
the way got work delete table, recreate without key , indexes , add unique index in tim's answer. however, after adding pk id column once more i'm old behaviour id listed unique column. weird.
use create unique index or use the gui of ssms.
create unique nonclustered index [name_unique] on [dbo].[projects] ( name asc ) you have added unique constraint not unique index.
your code retrieves uniquecolumns(the single column name).
Comments
Post a Comment