C#操作代理和EF存储库范围问题

时间:2016-11-08 15:12:13

标签: c# entity-framework scope delegates repository

我在WebApi控制器上有一个Wrapper方法,它暴露了Action Assate。我用它来与服务层交谈。 Action传递一个UnitOfWork,它有我的EF存储库用于模型

问题是当我尝试填充从控制器传递给服务的模型时,更改虽然它们在服务层应用,但不会反映在Controller上。然而,注释掉的方法确实有效,我不明白为什么

  //In controller
   Unit(uw =>  officeService.Get3(uw, user, User.Identity.GetUserId().ToString()));
        return Ok(user);
  //the returned user does not have changes applied

 //Service
 public void Get3(UnitOfWork uw, Member model, string id, string include = null)
    {
            object m = uw.MemberRepository.Get(u => u.UserId == id).FirstOrDefault();
            model = (Member)m;
            //model.Created = m.Created;
            //model.ExpiryInDays = m.ExpiryInDays;
            // and so on...
    }

1 个答案:

答案 0 :(得分:1)

class TimelineCollectionViewFlowLayout: UICollectionViewFlowLayout { // MARK: - Init override init() { super.init() self.minimumLineSpacing = 0 } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func prepare() { if let collectionView = self.collectionView { collectionView.isPagingEnabled = false self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) self.scrollDirection = .horizontal } } override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint { print("hello") return proposedContentOffset } } 的引用按值传递,因此更改modelmodel的分配不会对调用者产生任何影响。你可以通过ref:

Get3

或者返回它(因为你似乎忽略了参数)。

public void Get3(UnitOfWork uw, ref Member model, string id, string include = null)

这需要public Member Get3(UnitOfWork uw, string id, string include = null) { // ... return model; } 的重载,接受Unit并返回结果。