C
C#3d ago
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;
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;
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;
}
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 }}

}
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?
16 Replies
Jiry_XD
Jiry_XDOP3d ago
I know I can probably add them later, outside of the initialization but I'd like to enforce that a machine needs to exist WITH options WITH a price.
Keswiik
Keswiik2d ago
You do not have to set Machine in your OptionMachine entity for this to work. Assuming your relationships aren't messed up, EF should handle this for you. https://learn.microsoft.com/en-us/ef/core/saving/related-data#adding-a-graph-of-new-entities I also don't know why you're not using auto props here
public class OptionMachine {
[ForeignKey("Machine")]
public int MachineId { get; set; } = default;
public Machine? Machine { get; set; } # nullable because you can load the junction entity without loading the nav props

[ForeignKey("Option")]
public int OptionId { get; set; } = default;
public Option? Option { get; set; }

[Required]
public decimal Price { get; set; } = default;
}
public class OptionMachine {
[ForeignKey("Machine")]
public int MachineId { get; set; } = default;
public Machine? Machine { get; set; } # nullable because you can load the junction entity without loading the nav props

[ForeignKey("Option")]
public int OptionId { get; set; } = default;
public Option? Option { get; set; }

[Required]
public decimal Price { get; set; } = default;
}
Jiry_XD
Jiry_XDOP2d ago
Gotcha thanks! One more question however: As you can see in my machine class I have only a List of OptionMachine But not a list of Option. Online I always saw that they add both the explicit Junction/Join entity and the entity itself?: https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-and-join-table-with-payload https://stackoverflow.com/questions/78766403/net-ef-core-how-to-correctly-insert-and-update-a-model-in-a-many-to-many-rela For example in above Post, they hold both a list of Tag and a list of PostTag
Stack Overflow
.NET EF Core : how to correctly insert and update a model in a many...
I am writing a .NET CRUD application with layered architecture with EF Core for ORM. I have two models (Players and Teams) stored in the database with a join table between them (Player in Team). Th...
Many-to-many relationships - EF Core
How to configure many-to-many relationships between entity types when using Entity Framework Core
Jiry_XD
Jiry_XDOP2d ago
Do you need both or is one fine?
Keswiik
Keswiik2d ago
Not sure, I always set my many-many relationships up to use an explicit join table
Jiry_XD
Jiry_XDOP2d ago
Okay thanks, do you use them both? A list of your item and a list of your junction table? For example this Player class has both, but I am not sure if its necesarry?:
No description
Keswiik
Keswiik2d ago
No, I only use the explicit join table in my entities There are probably cases where having access to the other nav collection would be useful, but I don't use it myself
Jiry_XD
Jiry_XDOP2d ago
Gotcha, so you only hold TeamPlayers in the image's case?
Keswiik
Keswiik2d ago
Yup
Jiry_XD
Jiry_XDOP2d ago
Great thanks, that's what I was wondering if it worked Thanks 😛
Keswiik
Keswiik2d ago
:PepoSalute:
Jiry_XD
Jiry_XDOP2d ago
True, thats why I wanted to omit it
Keswiik
Keswiik2d ago
one thing to note, though, if you are creating a new entity and it references other entities that already exist, you may only need to set the ID on your join entity say, for example, you are creating a new machine that references an existing option i would probably do something like
var newMachine = new Machine {
// stuff here
Options = new() {
new() {
OptionId = someExistingOption.Id
}
}
};
var newMachine = new Machine {
// stuff here
Options = new() {
new() {
OptionId = someExistingOption.Id
}
}
};
I've had times where the EF change tracker tried re-creating the entities specified if the nav property was set to something that already existed
Jiry_XD
Jiry_XDOP2d ago
Oh I see thanks for the tip 🙂 I'll put that to good use 😛
Keswiik
Keswiik2d ago
:PepoSalute: gl
Jiry_XD
Jiry_XDOP2d ago
Thanks 😋
Want results from more Discord servers?
Add your server