Page 1 of 1

Insert multiple rows from DataGridView

Posted: Mon 18 Apr 2011 13:28
by schriever
Hello,

I'm using VS2008, .NET 3.5 SP1 and dotconnect 6.0 for Oracle. The Database is Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit. I have one table with one primary key column and one date column. On insert, I pull the primary key from a sequence and set the date to the current date with a trigger. This is tested and works fine from within sqldeveloper.

Code: Select all

CREATE OR REPLACE TRIGGER "TRG_LDD_BIUR" 
  before INSERT OR UPDATE ON T_LINEDOWNDEVICES 
  REFERENCING NEW AS NEW OLD AS OLD 
FOR EACH row BEGIN 
  IF inserting THEN 
    IF :new.ldd_id IS NULL OR :new.ldd_id < 1 THEN
      SELECT seq_linedown.nextval INTO :new.ldd_id FROM dual;
    END IF;
    :new.erstdatum := sysdate;
  END IF;
  IF updating THEN
    :new.editdatum := sysdate;
  END IF;
END;
In VS I generate an entity model from the Database. I bind the table representation in the entity with a bindingsource to a datagridview.

Code: Select all

MyModel.MyEntities ent = new MyModel.MyEntities();
tLinedowndeviceBindingSource.DataSource = ent.TLinedowndevices;
If I insert *1* new row into the datagridview and then save the changes, everything works fine.

Code: Select all

dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
ent.SaveChanges();
When I create more than one new row, I get the following error:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
Besides the wording of the error message, not all rows are saved correctly.

In the ef model, I set the default value for the primary key column to -1. The Entity Key property is true. I tried to set "Store Generated Pattern": Identity/Computed - nothing worked. I also set the date column to Store Generated Pattern: Computed.

Even in the working case (insert 1 row) I am missing on thing: I would expect the computed columns to be read back from the database and displayed in the datagridview, at least when I call refresh on the form

Code: Select all

this.Refresh();
But this does not happen either.

Also, I would be curious about one thing - when I add a new row to the datagridview and call SaveChanges() on the entity, it is saved, but I do not see the new object in the entity collection. How do I achieve this?

Posted: Wed 20 Apr 2011 09:26
by AndreyR
Please take a look at this post in our blog. Hopefully, this will help with the first problem.
As for refreshing DataGridView, please take a look at this discussion, for example.

Posted: Wed 20 Apr 2011 10:48
by schriever
Thank you for your reply. I read this hint, but as I was using VS 2008, I thought it would not apply. I tested it, and it helped solve the problem.

Do I then still need to set the StoreGeneratedPattern property in the designer to obtain in the CSDL section the devart:StoreGeneratedPattern="Identity" entry?

Posted: Wed 20 Apr 2011 11:06
by AndreyR
This annotation attribute does not influence anything in VS 2008, so it's up to you.
Was the advice about BindingList helpful?