简体   繁体   English

实体框架代码优先:如何指定索引名称

[英]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. 但是,我无法以编程方式创建全文索引,因为EF给出的主键索引名称是随机的。 Is there a way to explicitly set this? 有没有办法明确设置这个?

For example, I have an Items class like below: 例如,我有一个如下所示的Items类:

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. 现在我想要EF为Tags属性创建一个完整的文本目录,所以我会使用DbContext.SqlCommand()方法调用这样的东西。

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. 但是,您可能会注意到,主键索引名称不是我预期的名称:PK_ Items _727E838B07020F21。 Is there anyway to explicitly specify this key? 反正明确指定这个键吗?

Thanks. 谢谢。

Try using [Key] attribute ? 尝试使用[Key]属性?

using System.ComponentModel.DataAnnotations;

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

EF不提供任何指定密钥名称的可能性,更不用说索引名称了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM