c# - EF Core 使用 Fluent API 按类型配置实体属性
问题描述
我有一个具有多个相同类型属性的实体。我想在同一个表中包含该类型的所有属性。
OwnsOne(o => o.property)
您可以通过调用实体来获取同一个表中的各个属性。但我想在该类型的所有属性上执行此操作。
假设我们有以下(工作)示例:
public class Animal
{
public int Id { get; set; }
public ProvidedLength LengthAtBirth { get; set; }
public ProvidedLength LengthAtDead { get; set; }
}
public class ProvidedLength
{
public DateTime SubmissionDate { get; set; }
public string Unit { get; set; }
public string Length { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
public DbSet<Animal> Animals { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AnimalConfiguration());
}
}
class AnimalConfiguration : IEntityTypeConfiguration<Animal>
{
public void Configure(EntityTypeBuilder<Animal> builder)
{
// TODO: Apply the OwnsOne configuration all properties of the "ProvidedLength" type
builder.OwnsOne(a => a.LengthAtBirth);
builder.OwnsOne(a => a.LengthAtDead);
}
}
我不想调用每个属性,而是想为ProvidedLength
类型执行此操作。
我试过builder.OwnsOne<ProvidedLength>()
and builderBuilder.Entity<ProvidedLength>.OwnsOne()
,但在这两种情况下你都需要提供 navigationProperty。
解决方案
推荐阅读
- c - 复利计算器给出错误答案
- python - 我怎样才能提高这个跳跃?
- javascript - 在 Express.js 中计算时如何发送多个响应?
- c++ - C++ 错误我无法拼凑:误解数据类型的限制?
- deep-linking - 如何在离子电容器项目中进行身份验证后重定向回应用程序登录页面
- mysql - mariadb (mysql) 子分区错误(子分区总数超过64)
- python - 在 Python 中使用补丁上下文管理器在类方法中模拟请求时遇到问题
- python-3.x - 如何重用 dict-list cookie 来使用 python 向网站发送请求?
- python - Python Selenuim - InvalidArgumentExpression:十六进制转义的意外结束
- flutter - Flutter - 如果从推送通知的标题中获取此关键字,请执行此操作