Mel
Mel

Reputation: 3128

Entity Framework Code First: How to specify the index name

I'm using code first to generate my database. I will also be using Full Text for searching. However, I cant create the full text index programatically because the primary key index name given by EF is random. Is there a way to explicitly set this?

For example, I have an Items class like below:

public class Item
{

    public string ItemId { get; set; }
    public string ShortName { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public string ImageUri { get; set; }
    public string Tags { get; set; }

    public int CategoryId { get; set; }
    public string LastModifiedBy { get; set; }

    public virtual User LastModifiedBy { get; set; }
    public virtual ItemCategory Category { get; set; }
    public virtual ICollection<ItemMetaDataValue> MetaData{get;set;}
    public virtual ICollection<PriceHistoryLog> PriceHistory { get; set; }
}

Now I want to EF to create a full text catalog for the Tags property so I would call something like this using the DbContext.SqlCommand() method.

CREATE FULLTEXT CATALOG [DEFAULT]WITH ACCENT_SENSITIVITY = OFF
AS DEFAULT
AUTHORIZATION [dbo]

CREATE FULLTEXT INDEX ON [dbo].[Items] KEY INDEX [PK__Items__727E838B07020F21] ON ([DEFAULT]) WITH (CHANGE_TRACKING AUTO)

ALTER FULLTEXT INDEX ON [dbo].[Items] ADD ([Tags])

ALTER FULLTEXT INDEX ON [dbo].[Items] ENABLE

However, as you might notice, the primary key index name is not what I expect it to be: PK_Items_727E838B07020F21. Is there anyway to explicitly specify this key?

Thanks.

Upvotes: 4

Views: 1317

Answers (2)

Devart
Devart

Reputation: 122032

EF does not provide any possibility to specify even the key name, let alone the index name.

Upvotes: 2

activebiz
activebiz

Reputation: 6228

Try using [Key] attribute ?

using System.ComponentModel.DataAnnotations;

[Key]
public string ItemId { get; set; }

Upvotes: 3

Related Questions