This is a migrated thread and some comments may be shown as answers.

DataAccess identity seed reverting

2 Answers 50 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jurica
Top achievements
Rank 1
Jurica asked on 04 Mar 2014, 06:50 AM
Hi,

I have an issue with identity key. I am inserting a new record in table with identity set on primary field. 

Because there are three tables in question that represent N:N relationship I am first inserting in primary key tables and than in table that represent N:N relationship between those tables.

To do that and to stay in same transaction (for rollback) I am using this code block:

try
            {
                Medias media = new Medias()
                {
                    Name = entity.Name,
                    Datum = entity.Datum,
                    IdTipMedija = entity.IdTipMedija
                };
                this.dataContext.Add(media);

                Multimedijas mm = new Multimedijas();
                this.dataContext.Add(mm);

                this.dataContext.FlushChanges();

                MedijaMultimedijas newMMLink = new MedijaMultimedijas()
                {
                    IdMedija = media.IdMedija,
                    IdMultimedija = mm.IdMultimedija,
                    KljucneRijeci = entity.KljucneRijeci
                };

                this.dataContext.Add(newMMLink);

                this.dataContext.SaveChanges(Telerik.OpenAccess.ConcurrencyConflictsProcessingMode.AggregateAll);

                return newMMLink;
            }
            catch (Exception ex)
            {
                this.dataContext..ClearChanges();
            }

I am using this.dataContext.FlushChanges(); to get the id of primary key table (medias) so that I can insert in foreign key table.

For foreign key table object in domain model it has been set DatabaseServerCalculated for Identity Mechanism.

The problem is:  if there is an exception in inserting the table (medias - with identity) the code line this.dataContext..ClearChanges(); does not revert identity seed on the table.
What is the  way to achieve this? Best regards, Jurica









2 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 06 Mar 2014, 01:17 PM
Hi Jurica,

 Reverting the identity value is technically not possible and is not designed to be reverted. You can find a good explanation regarding this at this StackOverlow question.

Why do you want to revert  the identity? What do you want to achieve?

Regards,
Ady
Telerik
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
0
Jurica
Top achievements
Rank 1
answered on 12 Mar 2014, 02:50 PM
I figured something like that is a fact but somehow I thought there is some "automatic"/other mechanism for doing DBCC checkident('table', ressed,int) inside dataaccess framework.

Thx.
Tags
Development (API, general questions)
Asked by
Jurica
Top achievements
Rank 1
Answers by
Ady
Telerik team
Jurica
Top achievements
Rank 1
Share this question
or