Ejemplo n.º 1
0
        //[UnitOfWork(IsDisabled = true)]
        //public int Import(List<Dictionary<string,string>> importData)
        //{
        //    int count = 0;
        //    foreach(var row in importData)
        //    {
        //        count += ImportRow(row);
        //    }
        //    return count;
        //}

        protected override int ImportRow(Dictionary <string, string> row)
        {
            var fmDescription = row["Description"];
            var count         = Repository.GetAll().AsNoTracking().Where(e => e.Description == fmDescription).Count();

            if (count > 0)
            {
                return(0);
            }

            try
            {
                FoodMaterial         entity       = new FoodMaterial();
                var                  categoryName = row["FoodMaterialCategory"];
                FoodMaterialCategory category     = InsertOrGetCategory(categoryName);
                row.Remove("FoodMaterialCategory"); //删除免import出错

                entity.Import(row);
                entity.FoodMaterialCategoryId = category.Id; // 必须用id,不能用
                ///entity.FoodMaterialCategory = category; // 必须用id,不能用,否则会插入异常

                Repository.Insert(entity); //相关数据会自动添加吗?
                return(1);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return(0);
            }
        }
Ejemplo n.º 2
0
 public async Task InsertAsync(ICategory dto)
 {
     FoodMaterialCategory entity = new FoodMaterialCategory()
     {
         Code = dto.Code,
         Name = dto.Name
     };
     await _repository.InsertAsync(entity);
 }
Ejemplo n.º 3
0
        private FoodMaterialCategory InsertOrGetCategory(string categoryName)
        {
            var category = _categoryRepository.GetAll().AsNoTracking().Where(c => c.Name == categoryName).FirstOrDefault();

            if (category == null)
            {
                category = new FoodMaterialCategory()
                {
                    Name = categoryName
                };
                category.Id = _categoryRepository.InsertAndGetId(category); //似乎必须加上!
            }

            return(category);
        }