1

I have a serious problem, i have an unique key on field in db, I use Oracle(Devart Provider).

First time i preform an Insert -> (_objectSet.Add(entity)) via my repository it's ok,

BTW: I use Code Only model and CTP5.

Then if i want to insert it again it's fires an error that i have a "unique key constraint" and it's also alright.

After that no matter what do i do it's always throws me the same error!

What is that and how to fix it?

Thank you in advance.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
hackp0int
  • 4,052
  • 8
  • 59
  • 95

3 Answers3

1

Are you trying to do an .Add(entity) with the same entity? Then you will get that error. If you want to change something in that entity just do the changes like "entity.rowname = value" and then save without trying to do an .Add(entity) and it should work just fine.

How you usually do it is something like this.

Create new row in database:

Entity entity = new Entity();
entity.value = value;
db.Entity.AddObject(entity);
db.SaveChanges();

Retrieve and edit a row:

var entity = db.Entity.SingleOrDefault(e => e.value == value);
entity.value = newValue;
db.SaveChanges();

You can also do something like this without problem

Entity entity = new Entity(); //creating a new Entity
entity.value = 1;             //Setting a value on the new Entity
db.Entity.AddObject(entity);  //Adding the Entity to the context
db.SaveChanges();             //Saving the record to the database
entity = db.Entity.SingleOrDefault(e => e.value == 2); //retrieving another, already added record 
entity.value = 5;             //Changing the value on the retrieved record
db.SaveChanges();             //Saving the change to the database
entity = new Entity();        //creating yet another new Entity
entity.value = 8;             //setting the value on the second new Entity
db.Entity.AddObject(entity);  //adding the Entity to the context
db.SaveChanges();             //Saving the second new Entity to the database

You can NOT do like this

var entity = db.Entity.SingleOrDefault(e => e.value == value);
entity.value = newValue;
db.Entity.AddObject(entity); //WRONG!
db.SaveChanges();

Or this

Entity entity = new Entity();
entity.value = value;
db.Entity.AddObject(entity);
db.SaveChanges();
entity.value = newValue;
db.Entity.AddObject(entity); //WRONG!
db.SaveChanges();

In this case it will try to insert the already tracked entity as a new row with the same key and it will complain that there is already a previous row with the same key by throwing an "unique key constraint" error.

mattsson
  • 1,132
  • 12
  • 18
  • As i've said above to the guy upstairs i do these changes and nothing. – hackp0int Mar 10 '11 at 09:42
  • Sorry i am maybe were misunderstood, the problem is much deeper. I have set my self a unique constraint, that if some one put's the data for example "personid" not (primary or unique key's) for the first time ef4 response ok it's sais that you have unique key constraint on this column, but if i change the "personid" for a different it's still says the same thing, it's work's until the first time "unique key constraint" were thrown after that it's stopped working. I think maybe it's something wrong with the Devart provider that holds the Cache but they are checking it, until then i am waiting. – hackp0int Mar 11 '11 at 22:15
  • Ahh.. Yes.. I totally misunderstood the question then. In this case you will have to (as you state in your own answer) detach the entity. – mattsson Mar 21 '11 at 08:18
0

@IamStalker, could you please specify some more details concerning the error?
If possible, please either post here or send us the sample code you are using to add and update an entity, or even a small test project with your DB objects POCO code.

Devart
  • 119,203
  • 23
  • 166
  • 186
  • i have sent you a link please if i'm doing something not as it's supose to be. – hackp0int Mar 10 '11 at 12:08
  • I have sent to Devart support three days ago an email with the project example and haven't received any answer, why? – hackp0int Mar 13 '11 at 20:15
  • @IamStalker, we have replied you by e-mail. The reason seems to be the Insert implementation. After adding code that performs Attach of the related entities everything works fine. – Devart Mar 14 '11 at 11:03
  • No it doesn't work, i have attached your code as you did and it's still doesn't work. – hackp0int Mar 15 '11 at 06:18
0

The answer is simpler then i thought, i just have to Dettach the entity from Context, After i receive an exception of duplicate entities or any exception on it's holds in the context, the right way is to Dettach the entity and that's it.

I've got this answer from Andrey in Devart.

hackp0int
  • 4,052
  • 8
  • 59
  • 95