I'm trying to define a unique key constraint in Entity Framework.
Unique key for Last_Name
property in this Author
table.
I'm using Entity Framework version 6.0.
- This answer lead me to that using Data Annotations
- This answer showing to do this using SQL command in context class
Trying to do this is easiest way. So I'm preferring Data Annotations, but I'm getting this error:
System.Data.SqlClient.SqlException: Column 'Last_Name' in table 'dbo.Authors' is of a type that is invalid for use as a key column in an index.
Code:
public class Author
{
public Author()
{
}
[Key]
public int Auth_Id { get; set; }
public string First_Name { get; set; }
[Index("IX_FirstAndSecond", 1, IsUnique = true)]
public string Last_Name { get; set; }
public string Biography { get; set; }
}