Entity Framework Element Code First: How to Specify an Index Name

First I use the code to create my database. I will also use the full text for the search. However, I cannot create a full text index programmatically because the primary key index name given by EF is random. Is there any way to explicitly set this?

For example, I have a Items class, as shown 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 EF to create a full text directory 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 may have noticed, the primary key index name is not what I expect from it: PK_Items_727E838B07020F21. Does this key need to be explicitly specified?

Thanks.

+4
source share
2 answers

EF makes it impossible to specify even the key name, not to mention the index name.

+2
source

Try using the [Key] attribute?

 using System.ComponentModel.DataAnnotations; [Key] public string ItemId { get; set; } 
+3
source

Source: https://habr.com/ru/post/1342957/


All Articles