实体框架6.1 - 使用INCLUDE语句创建索引 [英] Entity Framework 6.1 - Create index with INCLUDE statement

查看:107
本文介绍了实体框架6.1 - 使用INCLUDE语句创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,实体框架6.1的最新beta版本中提供了索引,是否可以在代码优先方法中创建一个等于此SQL语句的索引?

So now that Indexes are available in latest beta version of Entity Framework 6.1 is it even possible to create an index in code first approach that is equal to this SQL statement?

CREATE NONCLUSTERED INDEX [Sample1]
ON [dbo].[Logs] ([SampleId],[Date])
INCLUDE ([Value])


推荐答案

严格来说,在Code First Migrations中始终可以使用它,因为你可以在迁移中运行sql:

Strictly speaking it has been always possible in Code First Migrations because you can run sql in a migration:

   public partial class AddIndexes : DbMigration
    {
        private const string IndexName = "IX_LogSamples";

        public override void Up()
        {
            Sql(String.Format(@"CREATE NONCLUSTERED INDEX [{0}]
                               ON [dbo].[Logs] ([SampleId],[Date])
                               INCLUDE ([Value])", IndexName));

        }

        public override void Down()
        {
            DropIndex("dbo.Logs", IndexName);
        }
    }

但我意识到你实际上可能在问你是否可以使用引入的IndexAttribute 创建索引6.1,但有一个Include列 - 答案是No

But I realise that you are probably actually asking if you can create an index using the IndexAttribute introduced in 6.1, but with an Include column - the answer to that is "No"

这篇关于实体框架6.1 - 使用INCLUDE语句创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆