C#C
C#15mo ago
25 replies
Jiry_XD

✅ Seeding question

Hi all,

In my app I am using EF Core
I have a many to many relationship between machine and option
Every machine has a different price per option, so I added price as payload to the junction table.

Pseudocode:
Machine:
List<OptionMachine> options; // Only holding OptionMachine list and not of options because we only want to access the options with a price.
String description;

Option:
List<Machine> machines;


OptionMachine:
    public int MachineId { get; set; } = default!;

    private Machine machine = default!;
    public required Machine Machine
    {
        get => machine;
        set => machine = value ?? throw new ArgumentNullException();
    }


    public int OptionId { get; set; } = default!;

    private Option Option = default!;
    public required =Option Option
    {
        get => customOption;
        set => customOption = value ?? throw new ArgumentNullException();
    }


    private decimal price = default!;

    public required decimal Price
    {
        get;
        set;
    }


Now my question is how do I seed Machine?

Lets say I have some existing options:
var options = await dbContext.Options.ToListAsync();

//Machine to seed:
Machine machine = new(){
  description="MyTestMachine",
  options = { new(){ Machine = //how to reference machine here?, 
                    Option= options[0], price=500M }}
  
}


As you can see I cannot use the machine itself to seed the junction table, how do I go about doing this?
Was this page helpful?