dimanche 6 janvier 2019

Entity Framework Core single object instead of List

I have a question. Is it possible to force Entity Framework to make one instance of class instead of (for example) IEnumerable?

In my database, i want to have only one Farm instead of Farms. My Farm have all other List in it that i mentioned in my DBContext:

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

    public DbSet<Farm> Farms { get; set; } //i want to have one instance of farm
    public DbSet<Machine> Machines { get; set; }
    public DbSet<Stable> Stables { get; set; }
    public DbSet<Worker> Workers { get; set; }
    public DbSet<Cultivation> Cultivations { get; set; }
}

And my Farm class, that is a Singleton (class with private constructor only with GetInstance() method):

public class Farm
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual List<Stable> Stables { get; set; }
    public virtual List<Machine> Machines { get; set; }
    public virtual List<Worker> Workers { get; set; }
    public virtual List<Cultivation> Cultivations { get; set; }

    public Farm GetFarm() => farm;

    private Farm farm;
    private Farm() { }
}

So how to make one Farm in whole database in Code First EntityFramework Core?

Aucun commentaire:

Enregistrer un commentaire