[英]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.由于某种我不知道的原因,EF 控制台正在返回 Stackoverflow。 I think it's a problem with seeding CompanyType data because without it, the migrations and database update occurs normal.我认为这是播种 CompanyType 数据的问题,因为没有它,迁移和数据库更新会正常进行。 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: 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();
}
}
CompanyType class:公司类型类:
public class CompanyType : BaseEntity
{
public string Name { get; set; }
public string Description { get; set; }
public decimal TaxRate { get; set; }
}
Console output:控制台输出:
I found the solution.我找到了解决方案。 It was a very silly error in the BaseEntity class, since CompanyType inherits it.这是 BaseEntity 类中的一个非常愚蠢的错误,因为 CompanyType 继承了它。
In the get property I put the public string itself to return, instead of the "_createdAt" private field.在 get 属性中,我将公共字符串本身放入返回,而不是“_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; }
}
The exception happened not because the property CreatedAt was null, it doesn' t matter what was a value.发生异常并不是因为属性 CreatedAt 为空,值是什么并不重要。 The exception happened because CreatedAt called itself recursively non-stop and it caused a stack overflow发生异常是因为 CreatedAt 以递归方式不停地调用自身并导致堆栈溢出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.