using(Context ctx= new Context())
{
Country country= new Country();
country.Name="India";
ctx.Countries.Add(country);
ctx.submitChanges();
State state= new State();
state.Name="UP";
state.countryId=country.countryId;
ctx.States.Add(state);
ctx.submitChanges();
}
Note : Succesfully added Country and states values in database.
using(Context ctx= new Context())
{
Country country= new Country();
country.Name="India";
ctx.Countries.Add(country);
ctx.submitChanges();
State state= new State();
int a=0;
int total = 10/a;
state.Name="UP";
state.countryId=country.countryId;
ctx.States.Add(state);
ctx.submitChanges();
}
Note : Country added sucessfully but state are not added corresponding to country.
So use transactions.
try
{
using(TransactionScope ts= new TransactionScope())
{
using(Context ctx= new Context())
{
Country country= new Country();
country.Name="India";
ctx.Countries.Add(country);
ctx.submitChanges();
State state= new State();
state.Name="UP";
state.countryId=country.countryId;
ctx.States.Add(state);
ctx.submitChanges();
ts.Commit();
}
}
}
catch(Exception ex)
{
ts.RollBack();
}
No comments:
Post a Comment