If you use the StringLengthAttribute
data annotation to specify a max length on the string column that you're trying to index, that might work:
[Index("SourceText", IsUnique = true), StringLength(500)]
public string SourceText { get; set; }
However, I'm not entirely sure that that's the best solution. I don't know much about it, but apparently you can include large columns as part of a non-clustered index:
You can include nonkey columns in a nonclustered index to avoid the current index size limitations of a maximum of 16 key columns and a maximum index key size of 900 bytes. The SQL Server Database Engine does not consider nonkey columns when calculating the number of index key columns or the total size of the index key columns.
In a nonclustered index with included columns, the total size of the index key columns is restricted to 900 bytes. The total size of all nonkey columns is limited only by the size of the columns specified in the INCLUDE clause; for example, varchar(max) columns are limited to 2 GB. The columns in the INCLUDE clause can be of all data types, except text, ntext, and image.
but I wouldn't know off the top of my head how to do that with data annotations or the Fluent API.
Additional resources for including large columns in a non-clustered index
- How to resolve 900 key length limit index on the column which have datatype varchar(4096) in SQL Server 2005?
- 900 byte index size limit in character length
- Create Indexes with Included Columns