huycon1002
Senior Member
Mình đang có 1 api tạo báo giá dưới đây,mặc dù đã cho vào trong transaction rồi nhưng trong trường hợp function CreateDetails fail thì ko back lại những gì đã tạo trong function CreateQuotation mà vẫn lưu vào trong db.
Có cách nào để trong transaction call nhiều function,nếu 1 function lỗi thì back lại tất cả được không mọi ng?
Có cách nào để trong transaction call nhiều function,nếu 1 function lỗi thì back lại tất cả được không mọi ng?
C#:
[HttpPost]
[Route("CreateQuotation")]
public async Task<IHttpActionResult> CreateQuotation(Quotation quo)
{
System.Data.Entity.DbContextTransaction transaction = db.Database.BeginTransaction();
try
{
var id = await quo.CreateQuotation();
quo.CreateDetails(id);
transaction.Commit();
return Ok("Tạo thành công");
}
catch(Exception ex)
{
transaction.Rollback();
return Ok(ex);
}
}
C#:
public class Quotation
{
private PivotdataEntities db = new PivotdataEntities();
public QUOTATION quotation { set; get; }
public List<QUOTATION_DETAILS> details { set; get; }
public async Task<int> CreateQuotation()
{
QUOTATION newq = new QUOTATION();
newq = this.quotation;
newq.CREATE_DATE = DateTime.Now;
newq.QUOTATION_CODE = CreateQuotationCode("HN");
db.QUOTATIONs.Add(newq);
await db.SaveChangesAsync();
return newq.ID;
}
public void CreateDetails(int id)
{
foreach (var item in this.details)
{
QUOTATION_DETAILS newdetail = new QUOTATION_DETAILS();
newdetail = item;
newdetail.QUOTATION_ID = id;
db.QUOTATION_DETAILS.Add(newdetail);
db.SaveChanges();
}
}
public string CreateQuotationCode(string macongty)
{
string thamso = "";
if (macongty == "HN")
{
thamso = "BGHN";
}
else
if (macongty == "HCM")
{
thamso = "BGHCM";
}
Regex digitsOnly = new Regex(@"[^\d]");
string year = DateTime.Now.Year.ToString().Substring(2, 2);
string month = DateTime.Now.Month.ToString();
string day = DateTime.Now.Day.ToString();
if (month.Length == 1)
{
month = "0" + month;
}
if (day.Length == 1)
{
day = "0" + day;
}
string prefixNumber = thamso + year.ToString() + month.ToString() + day.ToString();
string SoChungTu = (from quo in db.QUOTATIONs where quo.QUOTATION_CODE.Contains(prefixNumber) select quo.QUOTATION_CODE).Max();
if (SoChungTu == null)
{
return thamso + year + month + day + "0001";
}
SoChungTu = SoChungTu.Substring(10, SoChungTu.Length - 10);
string number = (Convert.ToInt32(digitsOnly.Replace(SoChungTu, "")) + 1).ToString();
string result = number.ToString();
int count = 4 - number.ToString().Length;
for (int i = 0; i < count; i++)
{
result = "0" + result;
}
return thamso + year + month + day + result;
}
}