0
My Class 
public class Dept_Master { [Key] public int Dept_Id { get; set; } [Index(IsUnique =true)] public string Dept_Name { get; set; } public bool status { get; set; } } 

Error on Following

Server Error in '/' Application.

Column 'Dept_Name' in table 'dbo.Dept_Master' is of a type that is invalid for use as a key columnin an index.

Description: An unhandled exception occurred during the

execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Column 'Dept_Name' in table 'dbo.Dept_Master' is of a type that is invalid for use as a key column in an index.

enter image description here

1
  • 1
    I guess Dept_Name is (n)varchar(max) which makes it non eligible for unique constraint Commented Jun 14, 2016 at 7:37

2 Answers 2

1

When you use a VARCHAR(Max), we will get this error. Please try using:

[Column(TypeName = "VARCHAR")] [StringLength(n)] [Index(IsUnique = true)] public string Dept_Name { get; set; } 

where n is between 1 and 450.

Sign up to request clarification or add additional context in comments.

Comments

1

Two Way You can assign unique key

1) [Column(TypeName = "NVARCHAR")] [StringLength(2000)] [Index(IsUnique = true)] public string Dept_Name { get; set; } 2) [Column(TypeName = "VARCHAR")] [Index(IsUnique = true)] public string Dept_Name { get; set; } 

@do not required for String length varchar datatype

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.