cs0*_*815 4 entity-framework entity-framework-6
我知道这一点,它表明不可能首先通过代码创建具有非聚集索引的主键.这仍然是这样吗?
理想情况下,我想通过EntityTypeConfiguration指定我的主键(Guid)有一个非聚集索引,还有另一个列(int)带有聚簇索引.
使用EntityTypeConfiguration无法实现AFAIK.但是,您可以使用Code-First迁移执行此操作.工作范例:
public class Product
{
public Guid Id
{ get; set; }
public int Price
{ get; set; }
}
class AppDbContext : DbContext
{
public DbSet<Product> Products
{ get; set; }
}
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Products",
c => new
{
Id = c.Guid(nullable: false),
Price = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id, clustered: false)
.Index(t => t.Price, clustered: true);
}
public override void Down()
{
DropIndex("dbo.Products", new[] { "Price" });
DropTable("dbo.Products");
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
CREATE TABLE [dbo].[Products] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Price] INT NOT NULL,
CONSTRAINT [PK_dbo.Products] PRIMARY KEY NONCLUSTERED ([Id] ASC)
);
GO
CREATE CLUSTERED INDEX [IX_Price]
ON [dbo].[Products]([Price] ASC);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2985 次 |
| 最近记录: |