实体框架6.1:如何将聚簇索引更改为另一列? [英] Entity Framework 6.1: How to change clustered index to another column?
问题描述
public class Invoice
{
public Guid Id {得到;组; }
public int InvoiceNumber {get;组; }
public string Caption {get;组;
}
在我的映射文件中,我将聚集索引设置为 InvoiceNumber
并在 Id
列上设置一个非聚集索引。
public class InvoiceMapping:EntityTypeConfiguration< Invoice>
{
public InvoiceMapping()
{
HasKey(p => p.Id).Property(e => e.Id).HasColumnType(SqlDbType.UniqueIdentifier。 ToString())。IsRequired()
.HasColumnAnnotation(Index,new IndexAnnotation(new IndexAttribute(){IsClustered = false}));
属性(e => e.Caption).HasColumnType(SqlDbType.NVarChar.ToString());
属性(e => e.InvoiceNumber).HasColumnType(SqlDbType.Int.ToString())。HasColumnAnnotation(Index,
new IndexAnnotation(new IndexAttribute(){IsClustered = true,IsUnique =真正}));
}
}
迁移后,我生成的类看起来如:
CreateTable(
dbo.Invoices,
c => new
{
Id = c.Guid(nullable:false),
InvoiceNumber = c.Int(nullable:false),
Caption = c.String(maxLength:4000),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Id)
.Index(t => t.InvoiceNumber,unique:true ,clustered:true);
当我调用更新数据库
这个错误:
无法在表'dbo.Invoices'上创建多个聚集索引。在创建另一个之前删除现有的聚簇索引PK_dbo.Invoices。
-----更新---- -
Neil 答案是正确的,但是,我不需要编辑迁移代码,代码
.PrimaryKey(t => t.Id,null,true)
自动生成
EntityFamework将所有主键创建为聚簇索引,您上面的代码(映射)指定列具有主键和非聚集索引,然后尝试在主键已经聚集时将聚集索引添加到另一列。
AFAIK并且无需更改迁移,就无法创建非群集主键,您需要更改迁移
.PrimaryKey(t => t.Id,null,true)
I have an entity like this:
public class Invoice
{
public Guid Id { get; set; }
public int InvoiceNumber { get; set; }
public string Caption { get; set; }
}
In my mapping file, I set the clustered index on InvoiceNumber
and set a non-clustered index on the Id
column.
public class InvoiceMapping : EntityTypeConfiguration<Invoice>
{
public InvoiceMapping()
{
HasKey(p => p.Id).Property(e => e.Id).HasColumnType(SqlDbType.UniqueIdentifier.ToString()).IsRequired()
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsClustered = false }));
Property(e => e.Caption).HasColumnType(SqlDbType.NVarChar.ToString());
Property(e => e.InvoiceNumber).HasColumnType(SqlDbType.Int.ToString()).HasColumnAnnotation("Index",
new IndexAnnotation(new IndexAttribute() { IsClustered = true ,IsUnique = true}));
}
}
After migration, my generated class looks like:
CreateTable(
"dbo.Invoices",
c => new
{
Id = c.Guid(nullable: false),
InvoiceNumber = c.Int(nullable: false),
Caption = c.String(maxLength: 4000),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Id)
.Index(t => t.InvoiceNumber, unique: true, clustered: true);
When I call Update-Database
I get this error:
Cannot create more than one clustered index on table 'dbo.Invoices'. Drop the existing clustered index 'PK_dbo.Invoices' before creating another.
-----Update------
Neil answer is correct but id there any way that I don't need edit migration code and the code
.PrimaryKey(t => t.Id, null, true)
generate automatically?
EntityFamework creates all primary keys as clustered indexes, you code above (the mapping) is specifying that the column has a primary key and a non-clustered index, you then try adding a clustered index to another column when the primary key will already be clustered.
AFAIK and without changing the migration it is not possible to create a non-clustered primary key, you would need to change the migration
.PrimaryKey(t => t.Id, null, true)
这篇关于实体框架6.1:如何将聚簇索引更改为另一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!