tags:

views:

48

answers:

2

Hello,

In my mvc application I have 2 tables named: detail(srNo, ID, work), master(ID, name,plan) that are related with foreign key relation ship from detail to master using "ID" field. "ID" field is primary key of master table. "srNo" field is primary key of detail table.

From the "ID" field, this 2 tables are joined with foreign key relation ship.

Now the Problem is: While adding any row to database, we are first make entry in master table , then in details table. And sometimes due to some exception, when row is succesfully added in master table but can not add in detail table. Then I want to perform rollback from master table.

But when I want to delete the row with recently added ID value (grom linq to sql), which is added in master table it is giving me exception that forign key realtion ship is there .....

And at that time in detail table there is no row of that id field.

Thanks

A: 

it seams that data is inserted in child table also. first you have to delete from detail table using deleteallonsubmit(detail) and then from the master table. can you explain what is meant by exception, is it error or your validation

public ActionResult CreateRec(Master _Master, IEnumerable<Detail> Detaildata,FormCollection collection)
{
  MasterRepo _MasterRepo = new MasterRepo();
  if (ModelState.IsValid)
        {
            if (_detail != null)
            {
                foreach (var objdetail in DetailData)
                {
                    Detail _Detail = new Detail();
                    _Detail.FirstField = objdetail.FirstField;
                   _Detail.SecondField = objdetail.SecondtField;
                    _Master.Detail.Add(_Detail);
                }
            }
          _MasterRepo.Save();  
        }

}
Tassadaque
No data is inserted in child table when I am going to delete from master table:
aayushi soni
It is not giving any validation error, it is giving the error that can not delete where foreign key relation exist.
aayushi soni
are you calling one submitchanges for child and parent or both are persisted to database separately
Tassadaque
I have added my new answer as comment field capacity is less. Please see that answer. and help
aayushi soni
please edit your answer while posting code press code button from formating so that it can be readable
Tassadaque
it seams you are calling submitchanges twice you should call it once. then you don't need any roll back. if any error occur transaction will roll back
Tassadaque
I can not call submitchanges once, without submitchange to master table, i am not able get the object of master table and then this object I will assign in the detail table field in which foreign key realtion ship exist.
aayushi soni
See the above edite answer. it will give you some idea. I haven't tested this snippet but i used it and it should work in this way
Tassadaque
A: 

Here is the code

master objmaster= new master(); objmaster.id =count; objmaster.name= name1; .. objEntities.AddObject("master", objmaster); objEntities.SaveChanges(); try { detail objDetail = new detail() master objMaster = (from r in objEntities.master where r.Id == count select r).First(); objDetail.master = objMaster; // Suppose I get any (format, null pointer exception here) objDetail.folow = sfields[0]; //In case of exception this below code will not be excecuted

objEntities.AddObject("detail", objDetail); objEntities.SaveChanges(); } catch(Exception ex) {

//Here i want to delete the row which I have added in master table: // I write the code for this master objMasters= (from r in objEntities.master where r.Id == count select r).ToList(); objEntities.DeleteObject(objMasters); objEntities.SaveChanges(); }

aayushi soni