public static class Util { //生成10000条sqlserver测试数据 public static List<PersonDetail> Get10000PersonDetails() { var personDetailsList = new List<PersonDetail>(); for (int i = 0; i < 10000; i++) { personDetailsList.Add(new PersonDetail() { FirstName = "FN" + new Random().Next(int.MaxValue), LastName = "LN" + new Random().Next(int.MaxValue) }); } return personDetailsList; } //生成10000条ElasticSearch测试数据 public static List<PersonDetail> Get10000PersonDetailsWithID() { var personDetailsList = new List<PersonDetail>(); for (int i = 0; i < 10000; i++) { personDetailsList.Add(new PersonDetail() { Id = i * new Random().Next(99), FirstName = "FN" + new Random().Next(int.MaxValue), LastName = "LN" + new Random().Next(int.MaxValue) }); } return personDetailsList; } }
public static class Setting { public static Uri Node { get { return new Uri("http://localhost:9200"); } } //连接配置 public static ConnectionSettings ConnectionSettings { get { return new ConnectionSettings(Node, defaultIndex: "es-index-app"); } } }
Model层代码:
public partial class PersonDetail { public long Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
public partial class PersonContext : DbContext { static PersonContext() { Database.SetInitializer<PersonContext>(null); } public PersonContext() : base("Name=PersonContext") { } public DbSet<PersonDetail> PersonDetails { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //在重写OnModelCreating方法中则可以直接调用映射类,从而减少了OnModelCreating方法的复杂度,同时也增强了代码维护的可读性 modelBuilder.Configurations.Add(new PersonDetailMap()); //属性映射约定 } }
//Fluent API配置Configuration映射类 public class PersonDetailMap : EntityTypeConfiguration<PersonDetail> { public PersonDetailMap() { // 主键 this.HasKey(t => new { t.Id, t.FirstName, t.LastName }); // 属性 this.Property(t => t.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.Property(t => t.FirstName) .IsRequired(); this.Property(t => t.LastName) .IsRequired(); // 表 & 列 映射 this.ToTable("PersonDetails"); this.Property(t => t.Id).HasColumnName("Id"); this.Property(t => t.FirstName).HasColumnName("FirstName"); this.Property(t => t.LastName).HasColumnName("LastName"); } }
sqlserver脚本: