.net EF 事物 訂單流水號的生成 (一)

 

首先須要 添加 System.Transactions 程序集ide

數據表:測試

create table SalesOrder
(
ID int identity(1,1) primary key not null,
OrderNo nvarchar(50) not null,
OrderDate datetime not null,
UserName nvarchar(50) not null
)


create table FlowNo
(
ID int identity(1,1) primary key not null,
OrderNo nvarchar(50) not null,
OrderType nvarchar(50) not null,
Remark nvarchar(50) not null
)
insert FlowNo values ('SM160309-00001','SM','銷售訂單')

控制檯測試程序。spa

測試的時候 ,能夠 輸入 userName ,最大字符爲50,能夠測試 輸入50個字符以上的用戶名,會發現code

  var tempModel = db.SalesOrder.FirstOrDefault(); if (tempModel != null) tempModel.UserName = tempModel.UserName + orderNo.ToString().PadLeft(5, '0'); db.SaveChanges();
這段代碼 沒有 效果了。雖然是後面的代碼報錯,第一行的數據不會發生改變。
using System;
using System.Linq;
using System.Transactions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var tran = new TransactionScope())
                {
                    using (var db = new pengbgDBEntities())
                    {
                        var flowNo = db.FlowNo.FirstOrDefault(x => x.OrderType == "SM");
                        if (flowNo != null)
                        {
                            var orderNo = int.Parse(flowNo.OrderNo.Split('-')[1]) + 1;
                            string newOrderNo;
                            if (flowNo.OrderNo.Split('-')[0] != "SM" + DateTime.Now.ToString("yyMMdd"))
                            {
                                newOrderNo = "SM" + DateTime.Now.ToString("yyMMdd-") + 1.ToString().PadLeft(5, '0');
                            }
                            else
                            {
                                newOrderNo = "SM" + DateTime.Now.ToString("yyMMdd-") +
                                             orderNo.ToString().PadLeft(5, '0');
                            }
                            var userName = Console.ReadLine();
                            var tempModel = db.SalesOrder.FirstOrDefault();
                            if (tempModel != null)
                                tempModel.UserName = tempModel.UserName + orderNo.ToString().PadLeft(5, '0');
                            db.SaveChanges();
                            db.SalesOrder.Add(new SalesOrder
                            {
                                OrderDate = DateTime.Now,
                                OrderNo = newOrderNo,
                                UserName = userName
                            });
                            db.SaveChanges();

                            flowNo.OrderNo = newOrderNo;
                            db.SaveChanges();
                        }
                    }
                    tran.Complete();
                    Console.WriteLine("操做成功");
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            finally
            {
                Console.WriteLine("按任意鍵繼續");
                Console.ReadKey();
            }

        }
    }
}
相關文章
相關標籤/搜索