zoukankan      html  css  js  c++  java
  • code first 如何创建索引字段

    比较简单的办法:

     1 protected override void Seed(Context context)
     2 {
     3     CreateIndex(context, "ProductName", "StoreProduct", true);
     4 }
     5 
     6 private void CreateIndex(Context context, string field, string table, bool unique = false)
     7  {
     8             context.Database.ExecuteSqlCommand(String.Format("CREATE {0}NONCLUSTERED INDEX IX_{1}_{2} ON {1} ({3})",
     9                 unique ? "UNIQUE " : "",
    10                 table,
    11                 field.Replace(",", "_"),
    12                 field));
    13 }

    Entity Framework 6提供了Index属性来创建数据库中特定列的Index,如下所示:

     
    class Student
    {
        public Student()
        {
        }
    
        public int Student_ID { get; set; }
        public string StudentName { get; set; }
            
        [Index]
        public int RegistrationNumber { get; set; }
    }

    默认情况下,索引名称将为IX_ {属性名称}。 当然你也可以更改索引名称。

    可以通过指定IsClustered = true和唯一索引来指定IsUnique = true来使其成为聚簇索引。

    [Index( "INDEX_REGNUM", IsClustered=true, IsUnique=true )]
    public int RegistrationNumber { get; set; }
  • 相关阅读:
    MySQL-percona安装
    Oracle-19C PSU升级
    Oracle-内存管理机制
    学习进度第十二周
    十天冲刺10
    单词统计续
    十天冲刺9
    学习进度第十一周
    十天冲刺8
    十天冲刺7
  • 原文地址:https://www.cnblogs.com/yanglang/p/9518513.html
Copyright © 2011-2022 走看看