question

WaqarMehmood-2786 avatar image
0 Votes"
WaqarMehmood-2786 asked karenpayneoregon answered

Why is EF core 5.0 inserting duplicate records ?

Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when using AddAsync() method for adding data.

209122-image.png


dotnet-csharpdotnet-aspnet-core-generaldotnet-entity-framework-core
image.png (92.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you mean that the entire rows is duplicated, including the CreatedDate column?

Also note that the result of your Where is not valid if a row is added at the same time.

To exclude duplicates, the database should include the corresponding constraints (unique indices).


0 Votes 0 ·

no created datetime is new when inserting.
Like see it an example in image: Row(4,11,12,13) are same on different created datetime

209123-image.png


0 Votes 0 ·
image.png (460.9 KiB)

Unrelated to the current issue, consider using shadow properties for create and modified properties in the DbContext.

See example where you just do the date time properties


0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon commented

Create a list of PurchaseSaleLog, then before saving using the debugger inspect values to ensure each element in the list is not a duplicate. Finally use AddRangeAsync to perform the add new records.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

did You mean this ?

210200-image.png


0 Votes 0 ·
image.png (135.0 KiB)

No, do the add range in the for statement and the save outside the for statement.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Here is a conceptual example for working with AddRange (works with AddRangeAsync also). DeviceType is a reference table used in other models. Yes it's not a for statement either but that does not matter. The point is we can set a breakpoint right before the SaveChanges and inspect data in the list which you would do outside the for statement before the save.

After the save all added items will have a key.

 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Threading.Tasks;
 using RelationalLevel1.Data;
 using RelationalLevel1.Models;
    
    
 namespace RelationalLevel1.Classes
 {
     public class PopulateOperations
     {
    
         public static async Task People()
         {
             List<DeviceType> deviceTypes = new()
             {
                 new() { Description = "Home phone" },
                 new() { Description = "Work phone" },
                 new() { Description = "Home email" },
                 new() { Description = "Work email" }
             };
    
             List<Person> people = new List<Person>
             {
                 new ()
                 {
                     FirstName = "Karen", LastName = "Payne",
                     Addresses = new List<Address>()
                     {
                         new () {Street = "123 Maple Street", PostalCode = "00022"},
                         new () {Street = "222 Apple street", PostalCode = "00022"},
                     }, 
                     ContactDevices = new List<ContactDevice>()
                     {
                         new () {DeviceTypeId = 1, Value = "503 999-1111"},
                         new () {DeviceTypeId = 3, Value = "karenp@gmail.com"}
    
                     }
                 },
                 new ()
                 {
                     FirstName = "Mary",
                     LastName = "Jones",
                     Addresses = new List<Address>()
                     {
                         new () {Street = "123 Cherry Street", PostalCode = "99999"},
                         new () {Street = "222 High Lane", PostalCode = "67654"},
                         new () {Street = "444 Birch Ave", PostalCode = "98234"},
                     },
                     ContactDevices = new List<ContactDevice>()
                     {
                         new () {DeviceTypeId = 1, Value = "503 555-5151"},
                         new () {DeviceTypeId = 3, Value = "mary@work.net"},
                         new () {DeviceTypeId = 2, Value = "971-987-9832"}
    
                     }
                 }
             };
    
             await using var context = new PersonContext();
             context.AddRange(deviceTypes);
             context.AddRange(people);
    
             await context.SaveChangesAsync();
    
         }
    
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.