© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
66 replies
UntoldTitan

EFCore Collection Trying to save a duplicate.

I'm building a dotnet maui app, and when I try to save a
Rental
Rental
with a
ICollection<Equipment>
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);
    }
    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; }
    }
    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();
        Rental createdRental = (Rental)result.Data;
        if (createdRental == null)
        {
            return;
        }
        db.Update(createdRental);
        await db.SaveChangesAsync();
        StateHasChanged();

At a break point at the
createdRental == null
createdRental == null
line, it shows that there are two equipments in the List.
It then throws an error on the
db.SaveChangesAsync()
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

AutoMapper.Collection.EFCore
C#CC# / help
4y ago
duplicate key error efcore
C#CC# / help
2y ago
EFCore - When to save changes
C#CC# / help
17mo ago
Trying to save a value
C#CC# / help
4y ago