实体框架code首先流利的API:添加索引的列 [英] Entity Framework Code First Fluent Api: Adding Indexes to columns
问题描述
我跑EF 4.2 CF和想在我的POCO对象来创建某些列的索引。
I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects.
作为一个例子,假设我们有这样的员工类:
As an example lets say we have this employee class:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
我们常常被他们的员工code做员工搜索和因为有很多的员工,这将是很好的有一个索引性能方面的原因。
We often do searches for employees by their EmployeeCode and since there are a lot of employees it would be nice to have that indexed for performance reasons.
我们能做到这一点用流利的API不知何故?或者数据注解?
Can we do this with fluent api somehow? or perhaps data annotations?
我知道这是可能执行SQL命令是这样的:
I know it is possible to execute sql commands something like this:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
我非常希望避免这样的原始的SQL。
I would very much like to avoid raw SQL like that.
我知道这并不存在,但在寻找类似的规定:
i know this does not exist but looking for something along those lines:
class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
internal EmployeeConfiguration()
{
this.HasIndex(e => e.EmployeeCode)
.HasIndex(e => e.FirstName)
.HasIndex(e => e.LastName);
}
}
或者使用 System.ComponentModel.DataAnnotations
的POCO看起来是这样的(再次我知道这不存在):
or maybe using System.ComponentModel.DataAnnotations
the POCO could look like this (again i know this does not exist):
public class Employee
{
public int EmployeeID { get; set; }
[Indexed]
public string EmployeeCode { get; set; }
[Indexed]
public string FirstName { get; set; }
[Indexed]
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
人对如何做到这一点的任何想法,或者有实施办法做到这一点任何计划时,code第一种方式?
Anyone have any ideas on how to do this, or if there are any plans to implement a way to do this, the code first way?
更新:作为由罗巴的答复中提到,此功能在EF版本中实现6.1
UPDATE: As mentioned in the answer by Robba, this feature is implemented in EF version 6.1
推荐答案
迁移后,在EF 4.3引入了修改或创建一个表时,你现在可以添加索引。下面是从<一个摘录href=\"http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-$c$c-based-migrations-walkthrough.aspx\">EF 4.3 $ C $基于C语言迁移演练从ADO.NET团队博客
After Migrations was introduced in EF 4.3 you can now add indexes when modifying or creating a table. Here is an excerpt from the EF 4.3 Code-Based Migrations Walkthrough from the ADO.NET team blog
namespace MigrationsCodeDemo.Migrations
{
using System.Data.Entity.Migrations;
public partial class AddPostClass : DbMigration
{
public override void Up()
{
CreateTable(
"Posts",
c => new
{
PostId = c.Int(nullable: false, identity: true),
Title = c.String(maxLength: 200),
Content = c.String(),
BlogId = c.Int(nullable: false),
})
.PrimaryKey(t => t.PostId)
.ForeignKey("Blogs", t => t.BlogId, cascadeDelete: true)
.Index(t => t.BlogId)
.Index(p => p.Title, unique: true);
AddColumn("Blogs", "Rating", c => c.Int(nullable: false, defaultValue: 3));
}
public override void Down()
{
DropIndex("Posts", new[] { "BlogId" });
DropForeignKey("Posts", "BlogId", "Blogs");
DropColumn("Blogs", "Rating");
DropTable("Posts");
}
}
}
这是一个很好的强类型的方法来添加索引,这是我一直在寻找,当我第一次张贴的问题。
This is a nice strongly typed way to add the indexes, which was what i was looking for when i first posted the question.
这篇关于实体框架code首先流利的API:添加索引的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!