This question is locked. New answers and comments are not allowed.
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
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