score:1
Don't forget to inform Entity Framework about your indexes. In your DbContext.OnModelCreating
:
modelBuilder.Entity<Person>()
.Property(person => person.Name)
.IsRequired()
.HasColumnAnnotation(IndexAnnotation.AnnotationName,
new IndexAnnotation(
new IndexAttribute("IndexPersonNames", 0)));
This is fairly unreadable. Because I had to do this for a lot of properties, I decided to create an extension function:
static PrimitivePropertyConfiguration HasIndex(
this PrimitivePropertyConfiguration property,
string indexName,
int columnOrder,
bool uniqueIndexValues = false)
{
var indexAttribute = new IndexAttribute(indexName, columnOrder)
{
IsUnique = uniqueIndexValues,
};
var indexAnnotation = new IndexAnnotation(indexAttribute);
return property.hasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation);
}
The above modelBuilder statement will be:
modelBuilder.Entity<Person>()
.Property(person => person.Name)
.IsRequired()
.HasIndex("indexPersonNames", 0)));
or if you want to index on two columns:
.IsRequired()
.HasIndex("indexSurName", 0) // first index by SurName
.HasIndex("indexFirstName", 1) // then by FirstName
Source: stackoverflow.com
Related Query
- Timeout Exception in SQL Server using via LINQ Query
- Using linq to query SQL Server is causing 100s of small queries
- Using LINQ to SQL to query large table (10M+ rows) results in timeout
- Query help for voting app in Linq using SQL Server
- EF LINQ spatial query times out using Contains() method in SQL Server
- using linq query with include/then_include vs using sql query with joins and gettng db server error
- Query a SQL Server table with multiple variables using LINQ and C# and return the results
- What can I do to resolve a "Row not found or changed" Exception in LINQ to SQL on a SQL Server Compact Edition Database?
- LINQ to SQL query using "NOT IN"
- How to Connect to SQL Server using LINQ to SQL?
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Ignoring accents in SQL Server using LINQ to SQL
- SQL Query to LINQ syntax using not exist and join
- Dynamic query using LINQ to SQL
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Check LINQ query against SQL Server database
- encapsulating logic in a linq to sql query via extension method
- Entity Framework: LINQ query generates different SQL between local execution and server execution
- How can I write the following code more elegantly using LINQ query syntax?
- Determine the source DataContext for a Linq to Sql query
- Search query gives LINQ to SQL exception
- Exception using CopyToDataTable with "new {..}" LINQ query
- Strange Exception thrown using Dynamic Linq Entity Framework Query
- How to get SQL query into LINQ form in C# code
- Using Linq to query linked server in Visual Studio
- Export SQL Server Database Table to XML Using Linq
- Converting Linq expression to sql server query
- LinQ to SQL throws Stackoverflow exception when using Any()
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Sql query to Linq using subquery
More Query from same tag
- Compare 2 lists with different values
- LINQ-ing with an ObservableCollection
- Format String to Currency in Linq
- surprising linq except behavior
- pagination in linq to sql into list
- Comparing i4o vs. PLINQ for larger collections
- Using Linq First Group particular Column value and then Make Avg of values from second column
- Filter Data table to list using Linq
- Setting property while enumerating over IOrderedEnumerable<> doesn't set property on object in collection
- LINQ Projection in Entity Framework
- Linq to entities is very slow using .Take() method
- Order by long Distance from Google Distance Matrix
- Using T-SQL AVG or taking Average after results returned using LINQ
- Query to many-to-many joiner table in EDMX
- How to get items from SQL, using linq to sql, where Date is from today to 30 days ahead and return as a list
- Need help with LINQ query
- How do I find the text within a div in the source of a web page using C#
- LINQ to XML - Load XML fragments from file
- vb.net LINQ LEFT JOIN
- Linq to Entities add Where clause for EXISTS lookup in another table
- C# linq build table from sub list of list
- When using the Linq methods .Any(<predicate>) and .All(<predicate>) on an IList, are the predicates applied to the list in a strict sequence?
- How do I Iterate Linq group result set?
- Extracting XML data, modifying it and storing in excel file
- Missing characters after string concatenate
- C# XML documentation of Linq to SQL
- Powershell equivalent of LINQ's Select command?
- Getting the error "cannot add an entity that already exists." while inserting a table's value in to DB without procedure
- Linq select statement but require no from line
- Why can't I use more than levels in a select linq statement?