Home > Blockchain >  Entity Framework Core 6 - IEntityTypeConfiguration Separate Configuration Class
Entity Framework Core 6 - IEntityTypeConfiguration Separate Configuration Class

Time:02-08

Using EF Core 6, a sequence can be set up in the model builder, and then use to generate values for properties following way.

//DBContex Class
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasSequence<int>("OrderNumbers");

    modelBuilder.Entity<Order>()
        .Property(o => o.OrderNo)
        .HasDefaultValueSql("NEXT VALUE FOR shared.OrderNumbers");
}

I'm trying to move the DbConfigurations to a model-specific seperate configuration class:

//Seperate Configuration Class
public class OrdertEntityConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.HasSequence("OrderNumbers").StartsAt(1000);

    }
}

But it gives following error:

Error CS1929 'EntityTypeBuilder' does not contain a definition for 'HasSequence' and the best extension method overload 'RelationalModelBuilderExtensions.HasSequence(ModelBuilder, string, string?)' requires a receiver of type 'ModelBuilder'

Any Solutions?

CodePudding user response:

A sequence does not belong to an entity, you must keep in main DbContext

  •  Tags:  
  • Related