ForSqlServerUseSequenceHiLo
method public class Category { public int Id { get; set; } public string Name { get; set; } }
Id
or < >Id
as a key. Now we need to create a DBContext: public class SampleDBContext : DbContext { public SampleDBContext() { Database.EnsureDeleted(); Database.EnsureCreated(); } protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder) { string onnString = @"Server=localhost\SQLEXPRESS01;Database=EFSampleDB;Trusted_Connection=true;"; optionBuilder.UseSqlServer(onnString); } protected override void OnModelCreating(ModelBuilder modelBuilder) { var entityTypeBuilder = modelBuilder.Entity<Category>(); entityTypeBuilder.ToTable("Category"); entityTypeBuilder.HasKey(t => t.Id); entityTypeBuilder.Property(t => t.Id).ForSqlServerUseSequenceHiLo(); entityTypeBuilder.Property(t => t.Name).HasMaxLength(128).IsRequired(); } public DbSet<Category> Category { get; set; } }
SampleDBContext
constructor SampleDBContext
which is the implementation of the DropCreateDatabaseAlways
database DropCreateDatabaseAlways
;OnConfiguring
method for configuring DBContext;OnModelCreating
method is the place where you can define the configuration of the model. To determine the Hi / Lo sequence, use the ForSqlServerUseSequenceHiLo
extension ForSqlServerUseSequenceHiLo
.ForSqlServerUseSequenceHiLo(string name, string schema)
. CREATE SEQUENCE [dbo].[EntityFrameworkHiLoSequence] AS [bigint] START WITH 1 INCREMENT BY 10 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807 CACHE GO
INCREMENT BY
option. In Sequence INCREMENT BY
adds the value to the previous value of the sequence to generate a new value. Thus, in this case, if your previous sequence value is 11, then the next sequence value will be 11 + 10 = 21. In the case of Hi / Lo Sequence, the INCREMENT BY
parameter denotes the block value, this means that the next sequence value will be selected after first use 10. using (var dataContext = new SampleDBContext()) { dataContext.Category.Add(new Category { Name = "Name-1" }); dataContext.Category.Add(new Category { Name = "Name-2" }); dataContext.Category.Add(new Category { Name = "Name-3" }); dataContext.SaveChanges(); }
dataContext.SaveChanges();
is called dataContext.SaveChanges();
All 3 categories will be saved with primary key values ​​that are already generated and are selected only once. protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ForSqlServerUseSequenceHiLo("DBSequenceHiLo"); }
ForSqlServerHasSequence
method to change the initial value and the increment value. But you can specify these values ​​as follows, first we define the sequence with the StartAt
and IncrementBy
parameters and then use the same ForSqlServerUseSequenceHiLo
extension ForSqlServerUseSequenceHiLo
. modelBuilder.HasSequence<int>("DBSequenceHiLo").StartsAt(1000).IncrementsBy(5); modelBuilder.ForSqlServerUseSequenceHiLo("DBSequenceHiLo");
CREATE SEQUENCE [dbo].[DBSequenceHiLo] AS [int] START WITH 1000 INCREMENT BY 5 MINVALUE -2147483648 MAXVALUE 2147483647 CACHE GO
IncrementBy
parameter is set to “5,” when the sixth entry is added to the context, a query will be made to the database to get the next sequence value. If you change the code, and add 3 more categories, then on the screenshot you can see the second database call.Source: https://habr.com/ru/post/349250/
All Articles