在Entity Framework Core中创建非聚集索引
使用Entity Framework Core,我希望拥有一个Guid PK,而不会在数据库中出现页面碎片 。
我看过这个post和这个 。 虽然在EF6中可行,但它的完成方式似乎已经发生了变化。
是否可以在Entity Framework Core中创建非群集主键并具有其他索引?
问答答以下。
可以使用EntityFrameworkCore v1.0.1或更高版本。
以下代码获得所需的结果:
using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; namespace TestApplication.Models { /// /// The context class. Make your migrations from this point. /// public partial class TestApplicationContext : DbContext { public virtual DbSet Companies { get; set; } public TestApplicationContext(DbContextOptions options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // standard stuff here... } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { entity.Property("CompanyId") .ValueGeneratedOnAdd(); entity.Property("CompanyIndex") .UseSqlServerIdentityColumn() .ValueGeneratedOnAdd(); entity.Property(e => e.CompanyName) .IsRequired() .HasColumnType("varchar(100)"); // ... Add props here. entity.HasKey(e => e.CompanyId) .ForSqlServerIsClustered(false) .HasName("PK_Company"); entity.HasIndex(e => e.CompanyIndex) .ForSqlServerIsClustered(true) .HasName("IX_Company"); }); } } /// /// The model - put here for brevity. /// public partial class Company { public Company() { } public Guid CompanyId { get; set; } public int CompanyIndex { get; set; } public string CompanyName { get; set; } // more props here. } }
Project.json
{ "version": "1.0.0-*", "dependencies": { "Microsoft.EntityFrameworkCore.Design": "1.0.1", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final", "NETStandard.Library": "1.6.0" }, "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final", "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { "netstandard1.6": { "imports": "dnxcore50" } } }
- 如何将图像放在C#中字节的图片框中
- C#使用通配符复制多个文件并保留文件名
- 如何在ASP.NET MVC 4中获取控制器操作的自定义注释属性?
- 我需要实现具有inheritance的C#深层复制构造函数。 有哪些模式可供选择?
- 如何反序列化为现有对象 – C#
- FileStream的同步要求。(开始/结束)(读/写)
- 将datagrid视图背景设置为透明
- COMException(0x800401E3):具有计划任务的操作不可用(来自HRESULT的exception:0x800401E3(MK_E_UNAVAILABLE))
- (OAuthException – #2500)必须使用活动访问令牌来查询有关当前用户的信息