I am trying to build a queue system in .NET 6 and Entity Framework where the end user will be able to create a location and add services to it. however, after I create a location I wont able to add a new service to the previously added location as the add service function will try to create a new location.
QueueLocation
entity:
public class QueueLocation
{
public int Id { get; set; }
public locationCity? City { get; set; }
public string? Hospital { get; set; }
public string? AddetionalDetails { get; set; }
public string? ImageUrl { get; set; }
public string? CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public string? UpdatedBy { get; set; }
public DateTime UpdatedAt { get; set; }
public int QueueMachineId { get; set; }
public ICollection<QueueMachine>? QueueMachines { get; set; }
public int QueueServiceId { get; set; }
public ICollection<QueueService>? QueueServices { get; set; }
}
QueueService
entity:
public class QueueService
{
public int Id { get; set; }
public string? ServiceNameEnglish { get; set; }
public string? ServiceNameArabic { get; set; }
public string? ServiceDescription { get; set; }
public string? CreatedBy { get; set; }
public string? ModifiedBy { get; set; }
public string? ServiceCode { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
public QueueLocation? QueueLocation { get; set; }
public ICollection<QueueMachine>? QueueMachines {get;set;}
public int QueueTicketId { get; set; }
public ICollection<QueueTicket>? queueTickets { get; set; }
}
DataContext
public class DataContext : IdentityDbContext <Account,IdentityRole,string>
{
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<QueueLocation> QueueLocation { get; set; }
public DbSet<QueueService> QueueServices { get; set; }
public DbSet<QueueMachine> QueueMachines { get; set; }
public DbSet<QueueTicket> QueueTicket { get; set; }
public DbSet<Account> accounts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<QueueService>()
.HasMany(e => e.QueueMachines)
.WithMany(e => e.QueueServices);
}
}
New location create method
public async Task<bool> CreateNewLocation(QueueLocation location)
{
await _context.QueueLocation.AddAsync(location);
return await _context.SaveChangesAsync() > 0 ? true: throw new BadRequestExceptions("Somthing Went Wrong");
}
New service create method:
await _context.QueueServices.AddAsync(Service);
return await _context.SaveChangesAsync() > 0 ? true : throw new BadRequestExceptions("Somthing Went Wrong");
In your code snippet ...
await _context.QueueServices.AddAsync(Service);
return await _context.SaveChangesAsync() > 0 ? true : throw new BadRequestExceptions("Somthing Went Wrong");
... you don't show how Service
was created.
This is the important bit.
My guess is that you are creating the QueueLocation property on Service without retrieving that QueueLocation from the database. This means that EF Core thinks it is a new object and will save a new instance accordingly.
The way to do it would be:
QueueLocation location = _context.QueueLocation.First(q => q.Id == queueId);
QueueService service = new QueueService();
service.QueueLocation = location;
await _context.QueueServices.AddAsync(service);
Using that code, EF Core will recognise that the Location is an existing Location and not create a new instance.