I have 2 entities, let's say A and B. Relation between them is many-to-many so I have another entity, let's say C.
Columns for table A:
-Id (PK) -> Type int generated by database
-PropertyA1
-PropertyA2
-PropertyA3
Columns for table B:
-Id (PK) -> Type int generated by database
-Description
Columns for table C (For this table I am not sure if it is better to add an extra column Id generated by database as previous tables):
-IdA (PK and also foreign key to entity A)
-IdB (PK and also foreign key to entity B)
Table B has fixed values that are inserted on seed method (overrided). Its entries are like below:
Id Description
1 "Some description 1"
2 "Some description 2"
3 "Some description 3"
From a form, user introduces the information related to table A (propertyA1,...,propeprtyA3) and then click on a button to save the data to the database.
Once user clicks on button's form to save data into database, first I do the following:
A a = new A(){ PropertyA1=something_1,
PropertyA2=something_2,
PropertyA3=something_3 };
context.A.Add(a);
context.SaveChanges();
then as after saving changes to database I have the Id generated by database (I have not id before saving to database), that is, a.Id, now I can proceed to add an entry to table C by doing:
B b = this.ObtainAppropriateB();
C c = new C(){ IdA = a.Id,
IdB = b.Id };
context.C.Add(c);
context.SaveChanges();
My problem is:
1) I cannot know a.Id previous to do context.SaveChanges after context.A.Add(a) because it is generated by database.
2) If context.SaveChanges fails after context.C.Add(c), how can I rollback as well previous work done?:
context.A.Add(a);
context.SaveChanges();
I cannot do the following because I have not a.Id previous to do SaveChanges:
A a = new A(){ PropertyA1=something_1,
PropertyA2=something_2,
PropertyA3=something_3 };
context.A.Add(a);
B b = this.ObtainAppropriateB();
C c = new C(){ IdA = a.Id,
IdB = b.Id };
context.C.Add(c);
context.SaveChanges(); <--- I call it once to persist changes to database
How to solve this?