首页 > 解决方案 > EF Core 在添加带有种子数据的迁移时返回 Stackoverflow

问题描述

我在应用带有一些数据的迁移时遇到问题。由于某种我不知道的原因,EF 控制台正在返回 Stackoverflow。我认为这是播种 CompanyType 数据的问题,因为没有它,迁移和数据库更新会正常进行。当我添加种子时,它开始返回这个。

应用数据库上下文:

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 配置类:

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();
    }
}

公司类型类:

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

控制台输出:

EF 控制台输出

标签: c#.netentity-framework

解决方案


我找到了解决方案。这是 BaseEntity 类中的一个非常愚蠢的错误,因为 CompanyType 继承了它。

在获取属性中,我将公共字符串本身返回,而不是“_createdAt”私有字段。

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; }
}

推荐阅读