Cannot attach an entity with the key that already exists.

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Cannot attach an entity with the key that already exists.

Post by ying515_huang » Mon 21 Feb 2011 04:01

I want to SubmitChanges(), in simple table, in LINQ to SQL is no problem.
but in dotconnect for Oracle, it have error message "Cannot attach an entity with the key that already exists." Why?

Code: Select all

        public ActionResult Index()
        {
            using (myDataContext db = new myDataContext())
            {
                TestDlT dl = new TestDlT();
                dl.DlId = 1;
                dl.HdId = 1;
                dl.Seq = 1;
                dl.ItemId = 1;
                dl.Qty = 1;
                db.TestDlTs.InsertOnSubmit(dl);

                TestDlT dl2 = new TestDlT();
                dl2.DlId = 1;
                dl2.HdId = 1;
                dl2.Seq = 1;
                dl2.ItemId = 1;
                dl2.Qty = 1;
                db.TestDlTs.Attach(dl2);    // <--   Error
                db.TestDlTs.DeleteOnSubmit(dl2);

                db.SubmitChanges();
            }
SourceCode https://docs.google.com/leaf?id=0B1l5ui ... 0&hl=zh_TW

ASP.NET MVC 2.0 & Visual 2010 & framework 4.0
Oracle DB 9i
dotConnect Ver 5.70.190.0
Last edited by ying515_huang on Tue 22 Feb 2011 05:47, edited 1 time in total.

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Mon 21 Feb 2011 15:25

This is a peculiarity of LinqConnect identity tracking: entities are attached to the data context immediately after the InsertOnSubmit method (apparently, LINQ to SQL attaches them only when the SubmitChanges method is executed). Could you please describe in more details the scenario in which such behaviour causes problems?

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Post by ying515_huang » Tue 22 Feb 2011 01:00

StanislavK wrote:This is a peculiarity of LinqConnect identity tracking: entities are attached to the data context immediately after the InsertOnSubmit method (apparently, LINQ to SQL attaches them only when the SubmitChanges method is executed). Could you please describe in more details the scenario in which such behaviour causes problems?
you can see my source code attachment file https://docs.google.com/leaf?id=0B1l5ui ... hl=zh_TW

Oracle Schema:
CREATE TABLE ERPPORTAL.TEST_DL_T
(
DL_ID NUMBER,
HD_ID NUMBER,
SEQ NUMBER,
ITEM_ID NUMBER,
QTY NUMBER
)
TABLESPACE SMP
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 1M
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING;
Last edited by ying515_huang on Tue 22 Feb 2011 05:46, edited 1 time in total.

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Post by ying515_huang » Tue 22 Feb 2011 05:41

ying515_huang wrote:
StanislavK wrote:This is a peculiarity of LinqConnect identity tracking: entities are attached to the data context immediately after the InsertOnSubmit method (apparently, LINQ to SQL attaches them only when the SubmitChanges method is executed). Could you please describe in more details the scenario in which such behaviour causes problems?
you can see my source code attachment file https://docs.google.com/leaf?id=0B1l5ui ... hl=zh_TW


the LINQ to SQL Source Code https://docs.google.com/leaf?id=0B1l5ui ... h&hl=zh_TW


MS SQL Schema:
CREATE TABLE [dbo].[TEST_DL_T](
[DL_ID] [int] NOT NULL,
[HD_ID] [int] NOT NULL,
[SEQ] [int] NOT NULL,
[ITEM_ID] [int] NOT NULL,
[QTY] [int] NOT NULL,
CONSTRAINT [PK_TEST_DL_T] PRIMARY KEY CLUSTERED
(
[DL_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Tue 22 Feb 2011 12:27

I meant, could you please describe the situation in which you need two different objects representing the same entity?

Attaching entities at the InsertOnSubmit call is the designed behaviour. If you encounter problems with it in one of your application scenarios, please describe this scenario so that we can suggest a solution.

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Post by ying515_huang » Wed 23 Feb 2011 00:33

StanislavK wrote:I meant, could you please describe the situation in which you need two different objects representing the same entity?

Attaching entities at the InsertOnSubmit call is the designed behaviour. If you encounter problems with it in one of your application scenarios, please describe this scenario so that we can suggest a solution.

when user add a row that it is not SubmitChange(), so he find incorrect and went delete it!

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Post by ying515_huang » Wed 23 Feb 2011 00:33

StanislavK wrote:I meant, could you please describe the situation in which you need two different objects representing the same entity?

Attaching entities at the InsertOnSubmit call is the designed behaviour. If you encounter problems with it in one of your application scenarios, please describe this scenario so that we can suggest a solution.

when user add a row that it is not SubmitChange(), so he find incorrect and want delete it!

why dotconnect LINQ can not db.TEST_DL_T.Attach(dl2) ?
the MS SQL LINQ can db.TEST_DL_T.Attach(dl2) ?

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Wed 23 Feb 2011 16:25

LinqConnect attaches new entities on InsertOnSubmit, whereas LINQ to SQL does this on SubmitChanges only. Thank you for your assistance, we will consider the possibility of changing this behaviour. We will post here when our investigation is completed.

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Post by ying515_huang » Thu 24 Feb 2011 02:11

StanislavK wrote:LinqConnect attaches new entities on InsertOnSubmit, whereas LINQ to SQL does this on SubmitChanges only. Thank you for your assistance, we will consider the possibility of changing this behaviour. We will post here when our investigation is completed.
We already buy dotConnect for Oracle.
What time can fix this problem?
It is urgent, thanks!

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Thu 24 Feb 2011 14:17

We will post here as soon as our investigation is completed, but cannot provide any timeframe for this at the moment.

Could you please describe the situation in more details so that we are able to suggest a solution or a temporary workaround? E.g., what controls are used to insert/delete entities, and at what moment the SubmitChanges method is executed? In particular, please specify why the original entity ('dl' in your sample) cannot be passed to the DeleteOnSubmit method.

listonic
Posts: 39
Joined: Mon 06 Dec 2010 15:47

Post by listonic » Fri 25 Feb 2011 08:50

I have a similar problem. I have a part of code which worked in dotconnect for postgresql and does not work in linqconnect.

I am working on detached entities. I fetch an entity from database, translate it into my buissiness layer object, work with the object then translate it to linq object to save it to database.

My scenario is like this
I do

var res = (from i in Table
where i.Query == query && i.SessionId == sessionId select new UserUniChoice(i)).FirstOrDefault();

then work with UserUniChoice object which is business layer object.

Then i try to


U ent = (U)Translator.FromEntity(entity);
UpdatableTable.Attach(ent, true);
Context.SubmitChanges();

This gives entity already attached error.
I am very concerned becuase it seems that the behaviour has changed (While translating from dotconnect to linq connect) which breaks my application.

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Fri 25 Feb 2011 18:13

This is the designed behaviour: provided that the row was already retrieved from the database, and the corresponding entity was attached to DataContext, it should not be possible to attach another entity with the same key.

To implement the approach you've described, you can use another DataContext instance for the update operation:

Code: Select all

U ent = (U)Translator.FromEntity(entity); 
MyDataContext newContext = new MyDataContext();
newContext.UpdatableTable.Attach(ent, true); 
newContext.SubmitChanges();
As no entities are attached to the newly created DataContext, you can attach 'ent' to it and perform the update.

listonic
Posts: 39
Joined: Mon 06 Dec 2010 15:47

Post by listonic » Mon 28 Feb 2011 09:55

Ok but you break all currently working applications. I cannot simply update dotconnect for postgresql to linq connect becuase it breaks everything

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Post by StanislavK » Mon 28 Feb 2011 12:59

Please specify the version of dotConnect for PostgreSQL with which you were working. If possible, please also send us a small complete sample where the scenario you've described is performed.

ying515_huang
Posts: 13
Joined: Mon 21 Feb 2011 03:35

Have anybody help me?

Post by ying515_huang » Wed 13 Apr 2011 08:19

StanislavK wrote:Please specify the version of dotConnect for PostgreSQL with which you were working. If possible, please also send us a small complete sample where the scenario you've described is performed.
Have anybody help me?

Post Reply