5

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?

EDIT

Maybe i wont 100% accurate with my question.

How to get single instance of Farm every time, i call a context? For example, i have a GET function:

private readonly FarmDbContext _context;
public FarmController(FarmDbContext context) => _context = context;


// GET: api/Farm
[HttpGet]
public IActionResult GetFarms() => Ok(_context.Farms.SingleOrDefault());

Can i call my Farm.GetFarm() => this.farm from DBContext?

3
  • 1
    An ORM is to map tables to entities, and a table is inherently not meant to store a single row only Commented Jan 7, 2019 at 7:10
  • 1
    Add another property that returns Farms[0]? Commented Jan 7, 2019 at 7:12
  • Look into my edit Commented Jan 7, 2019 at 7:32

1 Answer 1

4

Maybe you need to level-up your hierarchy thinking by one, as you have one database, want one farm, make the database the farm and everything inside the database the properties of the single farm.. thus essentially when you write dbContext.Stables.Where... the dbContext IS the farm, the stables are only ever the stables of that one farm. If you want to make another farm, make another database

Sign up to request clarification or add additional context in comments.

3 Comments

Not really sure what you're trying to achieve, having a GetFarms method in a system that only has one farm?
To be 100% accurate with singleton design pattern, i need to use acces method to class getInstance(). Im asking ypu, is it possible to get it that i want to (to get farm by that method) using EF or eveny any ORM ?
A relational database conceptually does not support this “single item of a kind” idea. There are no singleton tuples in a database; the only thing would be to have a table that has only one item. But that would appear to be an implementation detail. I agree with Caius that you should attempt to move this hierarchy up. Or think about whether you actually want a real singleton here (and as such limit you to never be able to extend your application to support multiple farms).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.