[英]How to create multi-columns “reference” index (join index) in c# model with EF6
I know how to create common multi-columns index in c# which is mapping table in database. 我知道如何在c#中创建通用的多列索引,该索引是数据库中的映射表。 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 : EF6将生成如下的t-sql:
create index MultipleIndexColumn on Table1(CreateDateTime)
which is not the expected sql sentence. create index MultipleIndexColumn on Table1(CreateDateTime)
这不是预期的sql语句。
here is my expected : create index MultipleIndexColumn on Table1(Table2_ID,Table3_ID,CreateDateTime)
这是我的预期:
create index MultipleIndexColumn on Table1(Table2_ID,Table3_ID,CreateDateTime)
Could you guys help about this? 你们可以帮忙吗?
You can create index keys
only on Primitive Datatypes
.So try as shown below. 您只能在
Primitive Datatypes
上创建index keys
。因此请尝试如下所示。
public class Table1
{
[ForeignKey("Table2_ID")]
public virtual Table2 Table2 { get; set; }
[Index("MultipleIndexColumn",1)]
public int Table2_ID { get; set; }
[ForeignKey("Table3_ID")]
public virtual Table3 Table3 { get; set; }
[Index("MultipleIndexColumn",2)]
public int Table3_ID { get; set; }
[Index("MultipleIndexColumn",3)]
public DateTime CreateDateTime {get; set;}
}
Thank https://stackoverflow.com/users/1077309/sampath for inspiring me. 感谢https://stackoverflow.com/users/1077309/sampath给我的启发。 Here is the Solution: as https://stackoverflow.com/users/1077309/sampath said You can create index keys only on Primitive Datatypes
解决方案如下:正如https://stackoverflow.com/users/1077309/sampath所说,您只能在原始数据类型上创建索引键
public class Table1
{
[ForeignKey("Table2_ID")]
public virtual Table2 Table2 { get; set; }
// here is important
[Index("MultipleIndexColumn",1)]
public int Table2_ID { get; set; }
[ForeignKey("Table3_ID")]
public virtual Table3 Table3 { get; set; }
// here is important
[Index("MultipleIndexColumn",2)]
public int Table3_ID { get; set; }
[Index("MultipleIndexColumn",3)]
public DateTime CreateDateTime {get; set;}
}
as the code The EF6 generate the index as I expected, create index MultipleIndexColumn on Table1(Table2_ID,Table3_ID,CreateDateTime) 作为代码EF6如我所料生成索引,在Table1(Table2_ID,Table3_ID,CreateDateTime)上创建索引MultipleIndexColumn
and The EF6 Didn't generate redundant column Table3No,Table2No in database. 并且EF6未在数据库中生成冗余列Table3No,Table2No。 That's perfect.
那很完美。
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:
您应该尝试将外部ID定义为单独的属性,并将其标记为:
public class Table1
{
public Table2 Table2Ref {get; set;}
public Table3 Table3Ref {get; set;}
[Index("MultipleIndexColumn",3)]
public DateTime CreateDateTime {get; set;}
[Index("MultipleIndexColumn",1)]
public int Table2Id {get; set;}
[Index("MultipleIndexColumn",2)]
public int Table3Id {get; set;}
}
or you could use the fluent api instead 或者您可以使用流利的api代替
You could use the Fluent API to define an index like that 您可以使用Fluent API这样定义索引
https://msdn.microsoft.com/en-us/data/jj591617.aspx?f=255&MSPPError=-2147217396#PropertyIndex https://msdn.microsoft.com/en-us/data/jj591617.aspx?f=255&MSPPError=-2147217396#PropertyIndex
For a complete list of the settings available in IndexAttribute, see the Index section of Code First Data Annotations.
有关IndexAttribute中可用设置的完整列表,请参见“代码优先数据注释”的“索引”部分。 This includes customizing the index name, creating unique indexes, and creating multi-column indexes.
这包括自定义索引名称,创建唯一索引以及创建多列索引。
https://msdn.microsoft.com/en-us/data/jj591583#Index https://msdn.microsoft.com/en-us/data/jj591583#Index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.