Ejemplo n.º 1
0
    /// <summary>
    /// 新增单个实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public virtual int Add(T entity)
    {
        Create(entity); //设置字段默认值
        //第一种方式
        context.Entry(entity).State = EntityState.Added;
        //第二种方式
        //context.Set<T>().Add(entity);

        // return context.SaveChanges();
        var result = 0;

        SaveAction(() =>
        {
            result = context.SaveChanges();
        });
        return(result);
    }
        public static string GetLZCode()
        {
            lock (_lock)
            {
                var dt    = DateTime.Now.Date;
                var data  = context.Set <LZCodeGenerate>().Where(m => m.CurrentDate >= dt).FirstOrDefault();
                var _code = "A";
                if (data != null)
                {
                    if (data.LZCode.Length > 7)
                    {
                        var _tempCode = data.LZCode.Substring(9, 1).ToUpper();
                        if (array.Contains(_tempCode))
                        {
                            var index = array.ToList().IndexOf(_tempCode);
                            _code = array[index + 1];
                        }
                    }
                    data.LZCode = $"{dt.ToString("yyyyMMdd")}_{_code}_KGB";
                    context.Entry(data).State = EntityState.Modified;
                    context.SaveChanges();
                }
                else
                {
                    LZCodeGenerate entity = new LZCodeGenerate()
                    {
                        LZCode      = $"{dt.ToString("yyyyMMdd")}_{_code}_KGB",
                        CurrentDate = dt
                    };
                    context.Entry(entity).State = EntityState.Added;
                    context.SaveChanges();
                }

                return($"{dt.ToString("yyyyMMdd")}_{_code}_KGB");
            }
        }