Entity Framework 6.1.1 Naming Convention for index

2019-04-10 04:38发布

问题:

I understand how to add conventions to the code first (with migrations) project. I have successfully managed to perform the table names and even changed GUID Id fields to non-clustered.

But I have not found how to change the default index name that EF supplies when no name is given.

[Index(IsUnique = true)]
public string Code { get; set; }

[Index]
public string Description { get; set; }

I have these two requirements. The top index should be named UX_[schema]_[table]_Code, the second IX_[schema]_[table]_Description

I also have the need to support multiple column indexes where IsUnique will still be UX but the column section is a Camel Case combination of all columns (e.g. UX_[schema]_[table]_CodeDescription if the two columns above where in the same index.

I'm assuming I would need to add this after the IndexAttributeConvention so that all the current functionality will work and create the IndexAnnotations. But I can't find where the Indexes are receiving their initial name if left blank in the Attribute (or Fluent construct).

Thanks in advance.

回答1:

Have you tried this annotation: [Index("IndexName")] or for your example:

[Index("IndexName", IsUnique = true)]
public string Code { get; set; }