2016-08-20 61 views
0

我無法使用EF更新我的數據庫(UpdateSpecified()方法不工作)。我的Add()方法工作正常。EF無法更新列成功時保存更改

編輯:我觀察到SaveChanges方法它總是返回1,它讓我感到困惑,因爲我更新了3個表。

我發現了RepositoryFactoryIQueryable代碼,它已經改變了:

enter image description here

我的代碼:

public class GoodsModel 
{ 
    private IRepositoryFactory _repositoryFactory; 
    private IServiceFactory _serviceFactory; 
    private IGoodsService _goodsService; 
    private IGoodsTypeService _goodsTypeService; 
    private IUsersService _userService; 
    private IOrderService _orderService; 

    private IOrdersRepository orderRepo; 
    private IUsersRepository userRepo; 
    private IGoodsRepository goodsRepo; 

    public GoodsModel() 
    { 
     _repositoryFactory = new RepositoryFactory(); 
     _repositoryFactory.OpenSession(); 
     _serviceFactory = new ServiceFactory(_repositoryFactory); 
     _goodsService = _serviceFactory.CreateGoodsService(); 
     _goodsTypeService = _serviceFactory.CreateGoodsTypeService(); 
     _userService = _serviceFactory.CreateUsersService(); 
     _orderService = _serviceFactory.CreateOrderService(); 

     userRepo = _repositoryFactory.CreateUsersRepository(); 
     orderRepo = _repositoryFactory.CreateOrdersRepository(); 
     goodsRepo = _repositoryFactory.CreateGoodsRepository(); 
     orderRepo = _repositoryFactory.CreateOrdersRepository(); 
    } 

    public bool BuyProduct(BuyProductDto model) 
    { 
     string name = HttpContext.Current.User.Identity.Name; 

     // _repositoryFactory.OpenSession(); 

     using (_repositoryFactory) 
     { 
      var user = _userService.Filter(x => x.UserName == name).FirstOrDefault(); 
      var good = _goodsService.Filter(x => x.GoodNumber == model.GoodNumber).FirstOrDefault(); 

      //var userRepo = _repositoryFactory.CreateUsersRepository(); 
      //var goodRepo = _repositoryFactory.CreateGoodsRepository(); 

      Users u = new Users(); 
      Goods g = new Goods(); 

      try 
      { 
       //substract when buy product. 
       int remainScore = user.UserScore - (int)good.GoodScore; 

       if (remainScore < 0) 
       { 
        return false; 
       } 

       u.UserId = user.UserId; 
       u.UserScore = remainScore; 

       userRepo.Attach(u); 
       userRepo.UpdateSpecified(u); 

       g.Id = good.Id; 

       //same as above syntax 
       g.GoodsQuantity = good.GoodsQuantity - 1; 

       goodsRepo.Attach(g); 
       goodsRepo.UpdateSpecified(g); 

       //orderRepo = _repositoryFactory.CreateOrdersRepository(); 

       orderRepo.Add(new Orders 
       { 
        GoodId = good.Id, 
        InsertTime = DateTime.Now, 
        Status = 0, 
        UserId = user.UserId, 
        OrderNo = DateTime.Now.ToString("yyyyMMddss"), 
        UpdateTime = DateTime.Now 
       }); 

       _repositoryFactory.Commit(); 

      } 
      catch (Exception ex) 
      { 
       _repositoryFactory.RollBack(); 
       return false; 
      } 
     } 

     return true; 
    } 
} 

框架的代碼是在這裏:

public void UpdateSpecified(T entity) 
{ 
     var type = entity.GetType(); 

     if (type.IsPrimitive || type == typeof(string)) 
     { 
      var props = type.GetProperties(); 

      foreach (var prop in props) 
      { 
       string propValue = prop.GetValue(entity, null) != null ? prop.GetValue(entity, null).ToString() : string.Empty; 

       if (!string.IsNullOrEmpty(propValue)) 
       { 
        DataContext.Entry<T>(entity).Property(prop.Name).IsModified = true; 
       } 
       else 
       { 
        DataContext.Entry<T>(entity).Property(prop.Name).IsModified = false; 
       } 
      } 
     }   
    }  

    public void Attach(T entity) 
    { 
     DataContext.Set<T>().Attach(entity); 
    } 
} 

像這樣的實體代碼,它c ontains導航屬性:

public class Users 
{ 
    public int UserId { get; set; } 

    public string UserName { get; set; } 
    public string Password { get; set; } 

    public DateTime InsertTime { get; set; } 

    public ICollection<UsersUserGroup> UsersUserGroups { get; set; } 

    public int UserScore { get; set; } 
} 

回答

0

橡木....我已經解決了它,它是傻瓜的問題,我承認;正確的代碼:

public void UpdateSpecified(T entity) 
    { 
     var props = entity.GetType().GetProperties(); 
     foreach (var prop in props) 
     { 
      if (prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(string)) 
      { 
       string propValue = prop.GetValue(entity, null) != null ? prop.GetValue(entity, null).ToString() : string.Empty; 
       if (!string.IsNullOrEmpty(propValue)) 
       { 
        DataContext.Entry<T>(entity).Property(prop.Name).IsModified = true; 
       } 
       else 
       { 
        DataContext.Entry<T>(entity).Property(prop.Name).IsModified = false; 
       } 
      } 
     } 

    }