I know how to create common multi-columns index in c# which is mapping table in database. But I encounter one specifical question on Multiple columns index, here is the code:
public class Table1
{
[Index("MultipleIndexColumn",1)]
public Table2 Table2_ID {get; set;}
[Index("MultipleIndexColumn",2)]
public Table3 Table3_ID {get; set;}
[Index("MultipleIndexColumn",3)]
public DateTime CreateDateTime {get; set;}
}
EF6 will generate t-sql like this :
create index MultipleIndexColumn on Table1(CreateDateTime)
which is not the expected sql sentence.
here is my expected :
create index MultipleIndexColumn on Table1(Table2_ID,Table3_ID,CreateDateTime)
Could you guys help about this?
You can create
index keys
only onPrimitive Datatypes
.So try as shown below.I suppose your trouble might be in using "the navigation properties" = the reference types. You should try to define the foreign IDs as separate properties and mark them like:
or you could use the fluent api instead
You could use the Fluent API to define an index like that
https://msdn.microsoft.com/en-us/data/jj591617.aspx?f=255&MSPPError=-2147217396#PropertyIndex
https://msdn.microsoft.com/en-us/data/jj591583#Index
Thank https://stackoverflow.com/users/1077309/sampath for inspiring me. Here is the Solution: as https://stackoverflow.com/users/1077309/sampath said You can create index keys only on Primitive Datatypes
as the code The EF6 generate the index as I expected, create index MultipleIndexColumn on Table1(Table2_ID,Table3_ID,CreateDateTime)
and The EF6 Didn't generate redundant column Table3No,Table2No in database. That's perfect.