Within my Blazor Web App .net 8 project I use Entity Framework Core. Below are a few classes I use in my project with all the necessary properties. I am trying for days to create a Razor page with an EditForm where I can create / update an Activity with all the child elements. But I did not manage to create a 100% working script. I got tracking errors, inserting Identity not allowed errors, etc ...
How to create a Razor page with .cs script to Add/Update an Activity and save all child entities? What is the correct way to collect existing items that can be added to the Activity. And what is the correct way to Add/Update this Activity?
Last script lines I tried:
Context = _dbContext.CreateDbContext();// I use this to collect existing items:// I also tried with .AsNoTracking()List<SalesItem> _salesItems = await Context.SalesItems.Where(x => x.Deleted == false).ToListAsync();List<Volunteer> _volunteers= await Context.Volunteers.Where(x => x.Deleted == false).ToListAsync();// I use this to add a new Activity:Context.Add(_activity);Activity.cs
public class Activity{ public string Name { get; set; } public List<VolunteerNecessity> VolunteerNecessities { get; set; } public List<SalesItemNecessity> SalesItemNecessities { get; set; }}VolunteerNecessity.cs
public class VolunteerNecessity{ public Volunteer Volunteer { get; set; } public int VolunteerId { get; set; } public string AdditionalInformation { get; set; } public VolunteerActivity VolunteerActivity { get; set; } public int VolunteerActivityId { get; set; }}SalesItemNecessity.cs
public class SalesItemNecessity{ public SalesItem SalesItem { get; set; } public int SalesItemId { get; set; } public string AdditionalInformation { get; set; } public int Amount { get; set; }}Volunteer.cs
public class Volunteer{ public string Name { get; set; }}VolunteerActivity.cs
public class VolunteerActivity{ public string Name { get; set; }}SalesItem.cs
public class SalesItem{ public string Name { get; set; }}