c# - SQL Server gives "duplicate key" error for a non-key property? -


i have db table products 5 columns. id primary key. in sql server management studio, can see here:

screenshot 1

i have product id = 69 , name = "lawn darts" . trying insert new product id = 420 , name = "lawn darts". trying use identity insert can specify id products inserted. names same id different. should no problem, right?

i using linq --> sql insert in c# console app. when try insert same name different id, following error message:

cannot insert duplicate key row in object 'dbo.products' unique index 'ix_name'. duplicate key value (lawn darts).

why, if non-key?

well, simpler making it. @rook pointers.

even though name column not primary key, specified "unique index". looking in wrong settings in sql server management studio, missed it. looking in "properties". needed right-click on "name" column , select "indexes/keys..." option. brings window can turn attribute is unique "no".

screenshot

alternatively, since using code-first migrations, can remove data-annotation property defined in c# , proceed update db via route.

before:

[index(isunique = true)] [maxlength(255)] public string name { get; set; } 

after:

[maxlength(255)] public string name { get; set; } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -