1

I am trying to insert duplicate record using EF by below code

db.Set<TEntity>().AddRange(lstEntity);
db.SaveChanges();

lstEntity is a list of type TEntity.

Suppose, I have 6 elements in lstEntity, 3 of them are duplicate.

On the first line in AddRange it only adds 4 elements, because 3 elements were duplicate so it adds 1 (3 duplicated) + 3 distinct records so it becomes 4.

And therefore, it inserts only 4 records in the DB.

I need to allow this duplicate thing and want to insert all the 6 elements (duplicate and distinct both).

8
  • Share some dummy data and more code. Commented Jul 13, 2016 at 7:02
  • you can't have duplicates in your database. If you want to know the number of a certain item add a Count column or something like that, or add another index column and add it to your PK. Most DBMS do not support duplicate PK's, and EF doesn't either. Commented Jul 13, 2016 at 7:57
  • PK's are not duplicate in this case, my PK is by default 0 which means insert. Technically all the elements are having PK as 0. Commented Jul 13, 2016 at 8:48
  • @MohammedDawoodAnsari : may I know what is the reason behind not using primary key? Performance ?\ Commented Jul 13, 2016 at 9:15
  • Primary is there at its place, I am inserting data here, so while inserting I keep the primary key value as 0 which ultimately means that I want to insert, if I will provide PK then it will be an update And in this case I am not updating, I am inserting values. P.S.: my PK is auto-generated identity in the database. Commented Jul 13, 2016 at 10:54

1 Answer 1

4

Entity Framework he is doing his job correctly, the object services will find the duplicated items and insert them one time in db. Think about it! an entity represents a row in your table with an Id. You cannot insert the same row multiple times, you need a new primary key(Id).

You can clone your entity and add it to the DbContext or use this

Your name is arabic so I think you can understand the arabic langauge just watch my tutorial this will answer all your quesitons:

Video


Example for Clone:

public class City
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int Id { get; set; }

  public City Clone()
  {
    return (City)MemberwiseClone();
  }

}

public class MyDbContext : DbContext
{
  public MyDbContext(string connectionString)
        : base("name=" + connectionString)
  {
  }

  public DbSet<City> Cities { get; set; }
}

public static void Main(string[] args)
{
  Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDbContext>());

  using (var myDbContext = new MyDbContext())
  {
    // This will add the city one time  => the bug
    var city = new City();

    var list = new List<City>();
    list.Add(city);
    list.Add(city);

    myDbContext.Cities.AddRange(list);
    myDbContext.SaveChanges();

    // This will add the city 2 times
    city = new City();
    var city2 = new City();

    list = new List<City>();
    list.Add(city);
    list.Add(city2);

    myDbContext.Cities.AddRange(list);
    myDbContext.SaveChanges();

    // This will add the clonned city1 and city=> Fix!
    var cityCloned1 = city.Clone();
    var cityCloned2 = city2.Clone();

    list = new List<City>();
    list.Add(cityCloned1);
    list.Add(cityCloned2);

    myDbContext.Cities.AddRange(list);
    myDbContext.SaveChanges();

  }

}

The result: enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

I understand, but what I mean is to insert all the duplicate records with different primary keys(auto-generated identity), so when I pass my list, all the element have pk as 0 which means to insert and Id should be autogenerated, I didn't mean to create 3 duplicate records with the same id, Id will be different but rest will be same
I am looking for some kind of configuration setting like dbContext.Configuration.AllowDuplicate = true, I dont want to hit database each time for each duplicate record.
duplicate for me when you are doing something like that: var list = new List<City>(); list.Add(city); list.Add(city); when you add the ctiy list now to the database the entity framework will track all attached entities he will need the object hashcode and in this case the city in the list has the same hashcode that why the EF will give them both one Id. the soultion is easy just create a clone extension and before you add it to the list clone it. #
@MohammedDawoodAnsari sorry i just found that you are from India the tutorial will not helping u
I tried simple cloning and having same problem... Is there any clean way to insert duplicate records at one time without hitting database multiple times. or I am not doing it correctly, it would be grateful for me if someone can provide me some sample code, if any
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.