Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when using AddAsync() method for adding data.
Why is Entity Framework 5.0 inserting duplicate records in the Database in uncertain behavior when using AddAsync() method for adding data.
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).
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
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
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.
No, do the add range in the for statement and the save outside the for statement.
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();
}
}
}
13 people are following this question.