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; }
}
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.
Can we do this with fluent api somehow? or perhaps data annotations?
I know it is possible to execute sql commands something like this:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
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);
}
}
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; }
}
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?
UPDATE: As mentioned in the answer by Robba, this feature is implemented in EF version 6.1
To build further on all these great responses, we added the following code to enable the Index attribute to be picked up from an associated metadata type. For the full details please see my blog post, but in summary here are the details.
Metadata types are used like this:
In this example the metadata type is a nested class, but it doesn't have to be, it can be any type. Property matching is done by name only, so the metadata type just has to have a property of the same name, and any data annotations applied to that should then be applied to the associated entity class. This didn't work in the original solution because it doesn't check for the associated metadata type. We plumbed in the following helper method:
For anyone using Entity Framework 6.1+, you can do the following with fluent api:
Read more in the documentation.
Note that in Entity Framework 6.1 (currently in beta) will support the IndexAttribute to annotate the index properties which will automatically result in a (unique) index in your Code First Migrations.
To build on frozen's response, you can hand code it into a migration yourself.
First, go to the Package Manager Console and create a new migration with
add-migration
, then give it a name. A blank migration will appear. Stick this in:Note that if you're using a string field it needs to be capped to a length of 450 chars as well.
Well i found a solution online and adapted it to fit my needs here it is:
Then overload OnModelCreating in your dbcontext
Apply the index attribute to your Entity type, with this solution you can have multiple fields in the same index just use the same name and unique.
If you want this feature added to EF then you can vote for it here http://entityframework.codeplex.com/workitem/57