Insert multiple rows from DataGridView

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
schriever
Posts: 2
Joined: Mon 18 Apr 2011 12:52

Insert multiple rows from DataGridView

Post by schriever » Mon 18 Apr 2011 13:28

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?

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Wed 20 Apr 2011 09:26

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.

schriever
Posts: 2
Joined: Mon 18 Apr 2011 12:52

Post by schriever » Wed 20 Apr 2011 10:48

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?

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Wed 20 Apr 2011 11:06

This annotation attribute does not influence anything in VS 2008, so it's up to you.
Was the advice about BindingList helpful?

Post Reply