【问题标题】:Create a non-clustered index in Entity Framework Core在 Entity Framework Core 中创建非聚集索引
【发布时间】:2017-04-21 14:51:37
【问题描述】:

使用Entity Framework Core,我想有一个Guid PK,而不用在数据库中遇到页面fragmentation

我见过postthis。虽然在 EF6 中是可能的,但它的完成方式似乎已经改变。

是否可以在 Entity Framework Core 中创建非聚集主键并具有附加索引?

问答如下。

【问题讨论】:

    标签: c# sql-server entity-framework entity-framework-core


    【解决方案1】:

    可以使用 EntityFrameworkCore v1.0.1 或更高版本。

    下面的代码得到了想要的结果:

    using System;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Metadata;
    
    namespace TestApplication.Models
    {
    
        /// <summary>
        /// The context class. Make your migrations from this point.
        /// </summary>
        public partial class TestApplicationContext : DbContext
        {
            public virtual DbSet<Company> Companies { get; set; }
    
            public TestApplicationContext(DbContextOptions<TestApplicationContext> options) : base(options)
            {
    
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                // standard stuff here...
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Company>(entity =>
                {
                    entity.Property<Guid>("CompanyId")
                            .ValueGeneratedOnAdd();
    
                    entity.Property<int>("CompanyIndex")
                            .UseSqlServerIdentityColumn()
                            .ValueGeneratedOnAdd();
    
                    entity.Property(e => e.CompanyName)
                        .IsRequired()
                        .HasColumnType("varchar(100)");
    
                    // ... Add props here.
    
                    entity.HasKey(e => e.CompanyId)
                        .ForSqlServerIsClustered(false)
                        .HasName("PK_Company");
                    entity.HasIndex(e => e.CompanyIndex)
                        .ForSqlServerIsClustered(true)
                        .HasName("IX_Company");
                });
            }
        }
    
            /// <summary>
            /// The model - put here for brevity.
            /// </summary>
            public partial class Company
            {
                public Company()
                {
                }
    
                public Guid CompanyId { get; set; }
                public int CompanyIndex { get; set; }
    
                public string CompanyName { get; set; }
                // more props here.
            }
    
        }
    

    项目.json

    {
        "version": "1.0.0-*",
    
        "dependencies": {
            "Microsoft.EntityFrameworkCore.Design": "1.0.1",
            "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
            "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
            "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final",
            "NETStandard.Library": "1.6.0"
        },
        "tools": {
            "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final",
            "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
        },
        "frameworks": {
            "netstandard1.6": {
                "imports": "dnxcore50"
            }
        }
    }
    

    【讨论】:

    • 在 EF Core 3+ 中它只是 .IsClustered(false)
    【解决方案2】:

    对于 EF Core 5 或更高版本

      // ...
      modelBuilder.Entity<MyEntity>(entity =>
      {
         // ...
         entity.HasIndex(e => e.MyIndexColumn)
           .IsClustered(false);
      }
      // ...
    

    =>此版本中删除了过时的方法ForSqlServerIsClustered

    对于 EF Core 3.x

      // ...
      modelBuilder.Entity<MyEntity>(entity =>
      {
         // ...
         entity.HasIndex(e => e.MyIndexColumn)
           .IsClustered(true); // new method
         // OR
         entity.HasIndex(e => e.MyIndexColumn)
           .ForSqlServerIsClustered(false); // obsolete method
      }
      // ...
    

    => 两种方法 IsClusteredForSqlServerIsClustered 都可以使用,但后面的一种已经被标记为过时,而取而代之的是第一种。

    对于 EF Core 1.x- EF Core 2.x

      // ...
      modelBuilder.Entity<MyEntity>(entity =>
      {
         // ...
         entity.HasIndex(e => e.MyIndexColumn)
           .ForSqlServerIsClustered(false);
      }
      // ...
    

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 2021-06-16
      • 2021-01-14
      • 2017-05-08
      • 2022-01-06
      • 2013-01-24
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      相关资源
      最近更新 更多