EFCore Collection Trying to save a duplicate.

I'm building a dotnet maui app, and when I try to save a Rental with a ICollection<Equipment> to my database, it errors saying that the equipment already exists.
Here's the models:
    public class Rental
    {
        public int Id {  get; set; }
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
        public ICollection<Equipment> Equipment { get; } = new List<Equipment>();
        public DateTime RentalDate { get; set; }
        public DateTime ReturnDate {  get; set; }
        [JsonIgnore]
        public float TotalCost => Equipment.Sum(p => p.Cost);
    }

Equipment:
    public class Equipment
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }
        [JsonIgnore]
        public string CategoryName => Category.Name;
        public string Name { get; set; }
        public string Description { get; set; }
        public float Cost { get; set; }
    }

Here's the code that saves it:
        Rental createdRental = (Rental)result.Data;
        if (createdRental == null)
        {
            return;
        }
        db.Update(createdRental);
        await db.SaveChangesAsync();
        StateHasChanged();

At a break point at the createdRental == null line, it shows that there are two equipments in the List.
It then throws an error on the db.SaveChangesAsync() line. I think I just need a way to tell EFCore to not save the elements in the list, but to just create a link to them from the Rental im trying to save.
image.png
image.png
Was this page helpful?