SqlException: Conversion failed when converting from a character string to uniqueidentifier

Question: How do you resolve this error "SqlException: Conversion failed when converting from a character string to uniqueidentifier." I am trying to make a number unique in the SQL Server database table?


Please Login to see the rest of the answer

Answer: If you want to make some intergers unique in the Sql Server Database table you need to check against existing records if one similar column value exist. The way you do this is by using Entity Framework 6 and LINQ to check if the data exist in the column table if yes then return an error message to the user.

See the code below:

if(!_context.MyEntity.Any(a => a.ColumnName == SomeValueYouWantToCompaireWith)){
//Value does not exist, go ahead and save your changes to the database.
}


If you use UniqueIdentifier as a Data type in your Database Table, the C# code expects a Guide to be mapped to that column, which you might not want in this case you need Numbers to be Unique in the Table Column.


Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy