0

So I've got this piece of code which, logically should work but Entity Framework is behaving unexpectedly.

Here:

foreach (SomeClass someobject in allObjects)
{
    Supplier supplier = new Supplier();

    supplier.primary_key = someobject.id;
    supplier.name = someobject.displayname;

    try
    {
       sm.Add(supplier);
       ro.Created++;
    }
    catch (Exception ex)
    {
       ro.Error++;
    }
}

Here's what I have in sm.Add()

    public Supplier Add(Supplier supplier)
    {
        try
        {
            _ctx.AddToSupplier(supplier);
            _ctx.SaveChanges();
             return supplier;
        }
        catch (Exception ex)
        {
            throw;
        }
    }

I can have records in allObjects that have the same id. My piece of code needs to support this and just move on to the next and try to insert it, which I think should work.

If this happens, an exception is throw, saying that records with dupe PKs cannot be inserted (of course). The exception mentions the value of the PK, for example 1000.

All is well, a new supplier is passed to sm.Add() containing a PK that's never been used before. (1001)

Weirdly though, when doing SaveChanges(), EF will whine about not being able to insert records with dupe PKs. The exception still mentions 1000 even though supplier contains 1001 in primary_key.

I feel this is me not using _ctx properly. Do I need to call something else to sync it ?

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
  • 1
    What is the structure of the Database fields ..? and why do all your records share the same ID.. there is definitely something incorrectly you are doing.. can you provide a schema of the table(s) layout..? – MethodMan Nov 21 '12 at 17:02
  • Where do you see I'm using the same ID ? It's a foreach loop containing objects and sometimes, some of them can have the same .id value which ends up being passed in supplier.primary_key. Not ALL of the records use the same ID. – Francis Ducharme Nov 21 '12 at 17:07
  • 1
    I was asking this because you have written this "I can have records in allObjects that have the same id. My piece of code needs to support this and just move on to the next and try to insert it, which I think should work." – MethodMan Nov 21 '12 at 17:10
  • Regardless, I need to be able to move on and insert other records that do not have a duplicate PK. But my context is remembering the previous PK that errored out when moving on to the next... – Francis Ducharme Nov 21 '12 at 17:12
  • take a look at this posting and refactor the way that you are doing your Inserts using Entity.. http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework – MethodMan Nov 21 '12 at 17:17

1 Answers1

0

Found it, had to change something in the Add() method:

public Supplier Add(Supplier supplier)
{
    try
    {
        _ctx.AddToSupplier(supplier);
        _ctx.SaveChanges();
         return supplier;
    }
    catch (Exception ex)
    {
        _ctx.Supplier.Detach(supplier);
        throw;
    }
}
Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81