简体   繁体   中英

EF Core returning Stackoverflow when adding migration with seeding data

I having an issue when applying a migration with some data on it. The EF console is returning Stackoverflow for some reason I don't know. I think it's a problem with seeding CompanyType data because without it, the migrations and database update occurs normal. When I added the seed, it started to return this.

AppDbContext:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions options) : base(options) { }

    public DbSet<Address> Addresses { get; set; }
    public DbSet<Taker> Takers { get; set; }
    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<Provider> Providers { get; set; }
    public DbSet<ServiceType> ServiceTypes { get; set; }
    public DbSet<CompanyType> CompanyTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new AddressConfiguration());
        modelBuilder.ApplyConfiguration(new CompanyTypeConfiguration());
        modelBuilder.ApplyConfiguration(new InvoiceConfiguration());
        modelBuilder.ApplyConfiguration(new ProviderConfiguration());
        modelBuilder.ApplyConfiguration(new ServiceTypeConfiguration());
        modelBuilder.ApplyConfiguration(new TakerConfiguration());

        modelBuilder.Entity<CompanyType>().HasData(
            new CompanyType()
            {
                Id = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow,
                Name = "Individual Business",
                Description = "This type of company is free from tax applications",
                TaxRate = 0.0M
            },
            new CompanyType()
            {
                Id = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow,
                Name = "Small Company",
                Description = "Small company that is beginning",
                TaxRate = 2.5M
            },
            new CompanyType()
            {
                Id = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow,
                Name = "Medium Company",
                Description = "Mid-port company",
                TaxRate = 4.5M
            },
            new CompanyType()
            {
                Id = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow,
                Name = "Large Company",
                Description = "Large company with more branches",
                TaxRate = 7.5M
            });

        base.OnModelCreating(modelBuilder);
    }
}

CompanyType configuration class:

public class CompanyTypeConfiguration : IEntityTypeConfiguration<CompanyType>
{
    public void Configure(EntityTypeBuilder<CompanyType> builder)
    {
        //The Id property inherits from the BaseEntity
        builder.HasKey(x => x.Id);

        builder.Property(x => x.Name)
            .IsRequired()
            .HasMaxLength(30);
        
        builder.Property(x => x.Description)
            .IsRequired();
        
        builder.Property(x => x.TaxRate)
            .IsRequired();
    }
}

CompanyType class:

public class CompanyType : BaseEntity
{
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal TaxRate { get; set; }
}

Console output:

EF 控制台输出

I found the solution. It was a very silly error in the BaseEntity class, since CompanyType inherits it.

In the get property I put the public string itself to return, instead of the "_createdAt" private field.

public abstract class BaseEntity
{
    [Key]
    public Guid Id { get; set; }
    private DateTime? _createdAt;
    public DateTime? CreatedAt
    {
      //get { return this.CreatedAt; } // Wrong!!!!!!!!! It will always be null!!
        get { return this._createdAt; } //Correct!
        set { _createdAt = (value == null ? DateTime.UtcNow : value); }
    }
    public DateTime? UpdatedAt { get; set; }
}

The exception happened not because the property CreatedAt was null, it doesn' t matter what was a value. The exception happened because CreatedAt called itself recursively non-stop and it caused a stack overflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM