1. however, for each entity of this model, i have to write a special function to DML it (so if i have 10 entities, i have to write a similar code for 10 times), like
Code: Select all
static DmlEntityA{
void Insert(entityA);
void Update(entiyA);
void Delete(entityA);
}
static DmlEntityB{
void Insert(entityB);
void Update(entiyB);
void Delete(entityB);
}
static DmlEntityC{
void Insert(entityC);
void Update(entiyC);
void Delete(entityC);
}
// ...
2. is there any generic way to do such things?
thx.
Code: Select all
private static bool PreserveChanges(MyModel.Clinic.MedRoute entity, DML dmlType)
{
using (var tx = new TransactionScope())
{
try
{
using (var ctx = new MyModel.Clinic.ClinicEntities())
{
switch (dml)
{
case DML.Insert:
ctx.AddToClinicObjects(entity);
break;
case DML.Update:
if (null != entity)
{
ctx.AttachTo("ClinicObjects", entity);
ctx.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
}
break;
case DML.Delete:
var oj2 = ctx.ClinicObjects.Where(x => x.Id == entity.Id).FirstOrDefault();
if (null != oj2)
{
ctx.DeleteObject(oj2);
}
break;
default:
break;
}
ctx.SaveChanges();
}
tx.Complete();
return true;
}
catch (System.Data.UpdateException ex)
{
// write error to log file
LogException(ex);
return false;
}
}
}