c# - 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; }
}
控制台输出:
解决方案
我找到了解决方案。这是 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; }
}
推荐阅读
- python - ModuleNotFoundError,竟然以为存在,而且已经安装
- java - 尝试检查用户选择的 ID 是否已在 Java 程序中选择
- c# - CTP1001 尝试构建 Docker 映像时发生错误
- apache-kafka - 如何重试 KStream 消息?
- kubernetes - RoleBinding 不授予权限
- web-scraping - 我在使用 Beautiful Soup 从 NCBI 网站上抓取数据时遇到困难
- php -
向 WooCommerce 结帐单选按钮表单字段选项添加html 标记 - c# - JSON Deserialize - 将常见类型的对象转换为列表
- java - JAXB 用点定义 XML 元素
- tensorflow - 老式 Keras 命令“Convolution2D”的现代语法?