EF Core 2.1.1 Seed data not working with override double

asp.net-core c# entity-framework-core seeding

Question

could you tell me why in my DB i get 0 values on after Seed method? This is how it looks like:

public static void Seed(this ModelBuilder modelBuilder)
    {
        int countOfWorkers = 10;

        for (int i = 0; i < countOfWorkers; i++)
        {
            modelBuilder.Entity<Driver>().HasData(
            new Driver
            {
                // 10 same workers
                Id = i + 1,
                FarmId = 1,
                FirstName = "name" + i,
                LastName = "lastname" + i,
                UsdPerHour = 7.2,
                HoursPerDay = 8,
                DaysOfWork = 31
            });
        }
    }

And my Models classes:

public abstract class Worker
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("Farm")]
    public int FarmId { get; set; }
    public Farm Farm { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual double UsdPerHour { get; set; }
    public virtual int HoursPerDay { get; set; }
    public virtual int DaysOfWork { get; set; }

    public virtual double CountBaseSalary() => throw new NotImplementedException();
}

public class Driver : Worker
{
    public Driver() => BaseSalary = CountBaseSalary();

    public override double UsdPerHour { get; set; }
    public override int HoursPerDay { get; set; }
    public override int DaysOfWork { get; set; }

    public override double CountBaseSalary() => UsdPerHour* HoursPerDay *DaysOfWork;

}

Im using EF Core 2.1.1. What i did wrong, that i get 0's values for propertys on Driver? If needed, i can add more info.

My DbContext:

public class FarmDbContext : DbContext
{
    public FarmDbContext(DbContextOptions<FarmDbContext> options) : base(options) { }

    public DbSet<Farm> Farms { get; set; }
    public DbSet<Worker> Workers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Driver>();
        builder.Seed();
        base.OnModelCreating(builder);
    }
}
1
0
1/8/2019 3:00:15 PM

Accepted Answer

If all Worker will have UsdPerHour, HoursPerDay, DaysOfWork then you do not need those fields specified in Driver, they will be inherited.

If only Driver needs those fields, then they should be removed from the base class Worker.

1
1/8/2019 3:44:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow