• 1400/02/01

استفاده از TransactionScope در Asp.net Core :

سلام استاد عزیز

یک سرویس برای افزودن کاربر دارم. در این سرویس باید درون 2 تا از جداول دیتابیس که با هم رابطه دارند، اطلاعات ذخیره شود. پس از ذخیره اطلاعات در یک جدول اگر عملیات ذخیره در جدول دوم با مشکل روبرو شود، چطور مقادیر ذخیره شده در جدول اولی پاک شوند؟

در MVC5 از TransactionScope استفاده میکردیم. کدها بصورت زیر هستند و با خطا مواجه میشم که هرچی سرچ کردم به نتیجه نرسیدم..

ممنون میشم اگر راهنمایی بفرمایید

public string CreateSupplier(vm_CreateSupplier supplier, IFormFile imgSupplierLogo)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    ApplicantsModel applicant = new ApplicantsModel();
                    string result = "";
                    string Identity = "";
                    bool Exist = true;
                    while (Exist == true)
                    {
                        string Identity_Part1 = RandomCharString(3);
                        string Identity_Part2 = Control.Date.PersianYear();
                        string Identity_Part3 = RandomNumberString(4);
                        Identity = Identity_Part1 + Identity_Part2 + Identity_Part3;
                        var check = _db.Applicants.Where(x => x.Identity.Equals(Identity)).FirstOrDefault();
                        if (check == null)
                        {
                            Exist = false;
                        }
                    }

                    applicant.Name = supplier.Name;
                    applicant.Family = supplier.Family;
                    applicant.FatherName = supplier.FatherName;
                    applicant.MeliCode = supplier.MeliCode;
                    applicant.ShShenasname = supplier.ShShenasname;
                    applicant.MobileNumber = supplier.MobileNumber;
                    applicant.UserName = supplier.UserName;
                    applicant.Password = supplier.Password;
                    applicant.Gender = supplier.Gender;
                    applicant.BirthDate = supplier.BirthDate;
                    applicant.Email = supplier.Email;
                    applicant.Address = supplier.Address;
                    applicant.PostCode = supplier.PostCode;
                    applicant.State = supplier.Ostan;
                    applicant.City = supplier.City;
                    applicant.RoleID = _db.Role.Where(x => x.Title.Equals("EshopSupplier")).Select(x => x.ID).FirstOrDefault();
                    applicant.RecommenderIdentity = supplier.RecommenderIdentity;
                    applicant.Identity = Identity;
                    applicant.State = "Active";
                    applicant.IsActive = true;
                    applicant.RegisterDate = Control.Date.GeorgianToPersian() + " - " + Control.Date.CurrentTime();
                    applicant.ActiveDate = Control.Date.GeorgianToPersian();
                    applicant.ActiveTime = Control.Date.CurrentTime();
                    applicant.ExpireDate = Control.Date.GeorgianToPersian(DateTime.Today.AddYears(+100));
                    applicant.ExpireTime = "12:00";
                    applicant.IsSupplier = true;
                    applicant.Authorized = true;

                    var phoneCheck = _db.Applicants.Where(x => x.MobileNumber.Equals(supplier.MobileNumber)).FirstOrDefault();

                    if (phoneCheck == null)
                    {
                        applicant.MobileNumber = supplier.MobileNumber;
                    }
                    else
                    {
                        transaction.Dispose();
                        return "-1";
                    }

                    var EmailCheck = _db.Applicants.Where(x => x.Email.Equals(supplier.Email)).FirstOrDefault();
                    if (EmailCheck == null)
                    {
                        applicant.Email = supplier.Email;
                    }
                    else
                    {
                        transaction.Dispose();
                        return "-2";
                    }

                    var UserCheck = _db.Applicants.Where(x => x.UserName.Equals(supplier.UserName)).FirstOrDefault();
                    if (UserCheck == null)
                    {
                        applicant.UserName = supplier.UserName;
                    }
                    else
                    {
                        transaction.Dispose();
                        return "-3";
                    }

                    var MeliCodeCheck = _db.Applicants.Where(x => x.MeliCode.Equals(supplier.MeliCode)).FirstOrDefault();

                    if (MeliCodeCheck == null)
                    {
                        applicant.MeliCode = supplier.MeliCode;
                    }
                    else
                    {
                        transaction.Dispose();
                        return "-5";
                    }

                    try
                    {

                        _db.Applicants.Add(applicant);
                        _db.SaveChanges();
                        var user = _db.Applicants.Where(x => x.UserName.Equals(applicant.UserName)).FirstOrDefault();
                        result = user.ID.ToString(); ;
                    }
                    catch(Exception e)
                    {
                        transaction.Dispose();
                        return "-4";
                    }

                    eShop_SupplierModel supplierModel = new eShop_SupplierModel();
                    if (Int32.Parse(result) > 0)
                    {
                        supplierModel.OwnerId = Int32.Parse(result);
                        supplierModel.CompanyName = supplier.CompanyName;
                        supplierModel.phoneNumber1 = supplier.phoneNumber1;
                        supplierModel.phoneNumber2 = supplier.phoneNumber2;
                        supplierModel.Description = supplier.Description;
                        supplierModel.Imagelogo = "DefaultSupplier.jpg";

                        if (imgSupplierLogo != null)
                        {
                            if (imgSupplierLogo.ContentType != "image/jpeg")
                            {
                                transaction.Dispose();
                                return "عکس ها باید با فرمت JPG باشند ، عکس  را بررسی نمایید.";
                            }
                            else
                            {
                                supplierModel.Imagelogo = Identity + "_" + Path.GetExtension(imgSupplierLogo.FileName);
                                string ImagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/E_Shop_Images/SupplierImages", supplierModel.Imagelogo);
                                using (var stream = new FileStream(ImagePath, FileMode.Create))
                                {
                                    imgSupplierLogo.CopyTo(stream);
                                }
                            }
                        }
                    }
                    else
                    {
                        transaction.Dispose();
                        return result.ToString();
                    }

                    _db.eShop_Suppliers.Add(supplierModel);
                    _db.SaveChanges();
                    transaction.Complete();
                    return "Ok";

                }
                catch (Exception e)
                {

                    transaction.Dispose();
                    return e.Message;
                }
            }
        }

خطا این هست:

The configured execution strategy 'SqlServerRetryingExecutionStrategy' does not support user initiated transactions. Use the execution strategy returned by 'DbContext.Database.CreateExecutionStrategy()' to execute all the operations in the transaction as a retriable unit.
  • 1400/02/01
  • ساعت 21:34

سلام 

مستندات Ef Core را مطالعه کنید ، کامل توضیح داده 

در دوره EF Core هم توضیح دادم


logo-samandehi logo-anjoman-senfi