I want check unique key constrain but I do not know which method to use. Which method is better? I use c#, EF, SQL Server
First Method:
bool contactExists = _db.Contacts.Any(contact => contact.ContactName.Equals(ContactName));
if (contactExists)
{
return -1;
}
else
{
_db.Contacts.Add(myContact);
_db.SaveChanges();
return myContact.ContactID;
}
Second Method:
Handle in exception.
The third method: check with T-SQL
If Exists (
Select Name
From dbo.ContentPage
Where Name = RTRIM(LTRIM(N'page 1'))
)
Begin Select 'True' End Else Begin Select 'False' End