Page 1 of 2

Cannot attach an entity with the key that already exists.

Posted: Mon 21 Feb 2011 04:01
by ying515_huang
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

Posted: Mon 21 Feb 2011 15:25
by StanislavK
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?

Posted: Tue 22 Feb 2011 01:00
by ying515_huang
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;

Posted: Tue 22 Feb 2011 05:41
by ying515_huang
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

Posted: Tue 22 Feb 2011 12:27
by StanislavK
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.

Posted: Wed 23 Feb 2011 00:33
by ying515_huang
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!

Posted: Wed 23 Feb 2011 00:33
by ying515_huang
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) ?

Posted: Wed 23 Feb 2011 16:25
by StanislavK
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.

Posted: Thu 24 Feb 2011 02:11
by ying515_huang
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!

Posted: Thu 24 Feb 2011 14:17
by StanislavK
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.

Posted: Fri 25 Feb 2011 08:50
by listonic
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.

Posted: Fri 25 Feb 2011 18:13
by StanislavK
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.

Posted: Mon 28 Feb 2011 09:55
by listonic
Ok but you break all currently working applications. I cannot simply update dotconnect for postgresql to linq connect becuase it breaks everything

Posted: Mon 28 Feb 2011 12:59
by StanislavK
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?

Posted: Wed 13 Apr 2011 08:19
by ying515_huang
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?