public void add(t_brand_product po)
        {
            crm1Entities context = new crm1Entities();
            //3.将改对象放入EF容器中,默认会为该对象加一个封装类对象(代理类对象)
            //用户对对象的操作,实际上是对代理类的操作
            //DbEntityEntry保存着实体状态,当对象被加入时,EF默认为该对象设置State的属性为unchanged
            DbEntityEntry <t_brand_product> entityEntry = context.Entry <t_brand_product>(po);

            //4.设置对象的标志位Added
            entityEntry.State = EntityState.Added;
            //5.当调用SaveChanges()时,EF会遍历所有的代理类对象,并根据标志生成相应的sql语句
            context.SaveChanges();
            //Console.WriteLine("添加成功");
        }
Esempio n. 2
0
        public void test1Test()
        {
            try
            {
                BaseInfo bi = new BaseInfo();
                bi.init();
                SearchService ss = new SearchService();

                BrandProductDao bpDao = new BrandProductDao();

                ReadProduct rf = new ReadProduct();
                if (null != ReadProduct.list)
                {
                    int i = 0;
                    foreach (string s in ReadProduct.list)
                    {
                        Debug.WriteLine(s);

                        string brand   = s.Split(',')[0];
                        string product = s.Split(',')[1];

                        string brand_code        = string.Empty;
                        string product_word_code = string.Empty;

                        brand_code = String.IsNullOrWhiteSpace(brand) ? "" : (null == ss.getWordBySynonym(brand)) ? "" : ss.getWordBySynonym(brand).code;
                        if (String.IsNullOrWhiteSpace(brand_code))
                        {
                            Debug.WriteLine("没查出brand_code" + brand);
                            i++;
                            Debug.WriteLine(i);
                            continue;
                        }

                        product_word_code = String.IsNullOrWhiteSpace(product) ? "" : ss.getWordBySynonym(product).code;
                        if (String.IsNullOrWhiteSpace(product_word_code))
                        {
                            Debug.WriteLine("brand_code = " + brand + " ; product_word_code = " + product + " 无值");
                        }
                        else
                        {
                            //判断是否重复
                            List <t_brand_product> list = bpDao.getproductByBrandAndProduct(brand_code, product_word_code);
                            if (list.Count > 0)
                            {
                                Debug.WriteLine("brand_code = " + brand + " ; product_word_code = " + product + " 重复");
                            }
                            else
                            {
                                t_brand_product bp = new t_brand_product()
                                {
                                    brand_code = brand_code, product_word_code = product_word_code
                                };
                                bpDao.add(bp);
                                Debug.WriteLine("brand_code = " + brand + " ; product_word_code = " + product + " 可以写入");
                            }
                        }

                        i++;
                        Debug.WriteLine(i);
                    }
                }
                else
                {
                    Debug.WriteLine("list = null" + " ; path = " + ReadProduct.path);
                }
            }
            catch (DbEntityValidationException ex)
            {
                Debug.WriteLine(ex.ToString());

                StringBuilder errors = new StringBuilder();
                IEnumerable <DbEntityValidationResult> validationResult = ex.EntityValidationErrors;
                foreach (DbEntityValidationResult result in validationResult)
                {
                    ICollection <DbValidationError> validationError = result.ValidationErrors;
                    foreach (DbValidationError err in validationError)
                    {
                        errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
                    }
                }
                Debug.WriteLine(errors.ToString());
                //简写
                //var validerr = ex.EntityValidationErrors.First().ValidationErrors.First();
                //Console.WriteLine(validerr.PropertyName + ":" + validerr.ErrorMessage);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }