带有 Seed 方法和 NotMapped 属性的 EF Core 2.1
EF Core 2.1 with Seed method and NotMapped attribute
我有一个问题,当我从上下文中获取所有 DbSet
时,为什么我的 Strategy
属性 为空?如何 [NotMapped]
属性 在我的后端可见?
我的 class 看起来像这样:
public class Machine
{
[Key]
public int Id { get; set; }
[NotMapped]
public WorkStrategy Strategy { get; set; }
public double GetManHours() => Strategy.TimeOfWork(HoursPerDay);
}
WorkStrategy
是一个摘要 class:
public abstract class WorkStrategy
{
public abstract double TimeOfWork(double hours);
}
public class FarmStrategy : WorkStrategy
{
public override double TimeOfWork(double hours) => // do things
}
public class CultivationStrategy : WorkStrategy
{
public override double TimeOfWork(double hours) => //do things
}
我的 Seed
播种机方法的一部分如下所示:
//Machines
for(int i = 0; i < countOfMachines; i++)
{
Machine machine = new Machine { Id = i + 1 };
machine.Strategy = new FarmStrategy;
modelBuilder.Entity<Machine>().HasData(machine);
}
但是当我从 DB 调用 Machines
时:
var machines = _context.Machines;
Strategy
属性 为空。你能告诉我,如何在播种数据库时附加 [NotMapped]
属性
?有可能吗?
编辑
当我想添加 WorkStrategy
而不是 "notmapped" 时,我在添加迁移时收到来自 EF 的错误:
The entity type 'WorkStrategy' requires a primary key to be defined
但我不想为 WorkStrategy 创建 table。
编辑
我的 OnModelCreating
在 context
class:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Machine>().Ignore(x => x.Strategy);
builder.Seed();
base.OnModelCreating(builder);
}
它不像 [NotMapped]
那样工作
你可以使用 fluent api ignore 而不是 notmapped
modelBuilder.Entity<Machine>().Ignore(x => x.Strategy );
我认为您的问题不在于未映射的属性,而是您 类 的结构。
如果你有一个标志,需要哪种类型的策略,并根据该标志调整策略-属性 以初始化策略(如果它为空),你可以保留你的 Notmapped-Attribute 或 Ignore-Method with Fluent-Api.
我有一个问题,当我从上下文中获取所有 DbSet
时,为什么我的 Strategy
属性 为空?如何 [NotMapped]
属性 在我的后端可见?
我的 class 看起来像这样:
public class Machine
{
[Key]
public int Id { get; set; }
[NotMapped]
public WorkStrategy Strategy { get; set; }
public double GetManHours() => Strategy.TimeOfWork(HoursPerDay);
}
WorkStrategy
是一个摘要 class:
public abstract class WorkStrategy
{
public abstract double TimeOfWork(double hours);
}
public class FarmStrategy : WorkStrategy
{
public override double TimeOfWork(double hours) => // do things
}
public class CultivationStrategy : WorkStrategy
{
public override double TimeOfWork(double hours) => //do things
}
我的 Seed
播种机方法的一部分如下所示:
//Machines
for(int i = 0; i < countOfMachines; i++)
{
Machine machine = new Machine { Id = i + 1 };
machine.Strategy = new FarmStrategy;
modelBuilder.Entity<Machine>().HasData(machine);
}
但是当我从 DB 调用 Machines
时:
var machines = _context.Machines;
Strategy
属性 为空。你能告诉我,如何在播种数据库时附加 [NotMapped]
属性
?有可能吗?
编辑
当我想添加 WorkStrategy
而不是 "notmapped" 时,我在添加迁移时收到来自 EF 的错误:
The entity type 'WorkStrategy' requires a primary key to be defined
但我不想为 WorkStrategy 创建 table。
编辑
我的 OnModelCreating
在 context
class:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Machine>().Ignore(x => x.Strategy);
builder.Seed();
base.OnModelCreating(builder);
}
它不像 [NotMapped]
你可以使用 fluent api ignore 而不是 notmapped
modelBuilder.Entity<Machine>().Ignore(x => x.Strategy );
我认为您的问题不在于未映射的属性,而是您 类 的结构。 如果你有一个标志,需要哪种类型的策略,并根据该标志调整策略-属性 以初始化策略(如果它为空),你可以保留你的 Notmapped-Attribute 或 Ignore-Method with Fluent-Api.