Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Can';t使用存储库模式和LINQ to SQL更新数据库_Asp.net Mvc_Linq_Repository - Fatal编程技术网

Asp.net mvc Can';t使用存储库模式和LINQ to SQL更新数据库

Asp.net mvc Can';t使用存储库模式和LINQ to SQL更新数据库,asp.net-mvc,linq,repository,Asp.net Mvc,Linq,Repository,我的项目具有以下设置 控制器->服务(用于验证)->存储库(用于LINQ)->dbml->数据库 控制器 ''# <AcceptVerbs(HttpVerbs.Post)> - hiding this line so that code formatting looks proper in SO. Function Edit(ByVal user As Domain.User, ByVal id As Integer) As ActionResult If ModelS

我的项目具有以下设置

控制器->服务(用于验证)->存储库(用于LINQ)->dbml->数据库

控制器

''# <AcceptVerbs(HttpVerbs.Post)>  -  hiding this line so that code formatting looks proper in SO.
Function Edit(ByVal user As Domain.User, ByVal id As Integer) As ActionResult

    If ModelState.IsValid Then

        If Not String.IsNullOrEmpty(user.UserName) AndAlso _
            Not String.IsNullOrEmpty(user.WebSite) AndAlso _
            Not String.IsNullOrEmpty(user.Email) AndAlso _
            Not String.IsNullOrEmpty(user.About) AndAlso _
            Not user.Region Is Nothing AndAlso _
            Not user.BirthDate Is Nothing AndAlso _
            Not user.isProfileComplete = True Then

            user.isProfileComplete = True
            user.Reputation = user.Reputation + 10

            UserService.UpdateUser(user)

        Else
            UserService.UpdateUser(user)
        End If

        UserService.SubmitChanges()

        Return RedirectToAction("Details", "Users", New With {.id = id, .slug = user.UserName})
    Else
        Return View(user)
    End If
End Function
存储库

    Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
        Dim _user = (From u In dc.Users
            Where u.ID = user.ID
            Select u).Single
        _user = user
    End Sub

    Public Sub SubmitChanges() Implements IUserRepository.SubmitChanges
        dc.SubmitChanges()
    End Sub
问题是它没有得到更新。我一定是在做什么蠢事。如有任何建议,将不胜感激

Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser 
    Dim _user = (From u In dc.Users 
        Where u.ID = user.ID 
        Select u).Single 
    _user = user 
End Sub 
在这个方法中,您从数据库获取用户。Linq2Sql正在跟踪该用户实例,以了解属性是否已更改。因此,在linq语句之后,用户被数据上下文所知,并且可以被跟踪。将用户参数指定给对象(
\u user=user
)时,将丢失对跟踪对象的引用,并将其替换为未跟踪对象。您可以单独更新包含数据库信息的
\u user
变量,也可以尝试将用户参数附加到数据上下文并直接使用(
dc.Users.attach(user)
)。附加可能很复杂,因为如果没有相同的datacontext实例,它将无法正确附加。下面是我将如何做到这一点

Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser 
    Dim _user = (From u In dc.Users 
        Where u.ID = user.ID 
        Select u).Single 
    With _user
        .Name = user.Name
        .Email = user.Email
    End With
End Sub 

我当然可以单独更新对象,但如何将用户参数附加到数据上下文?With语句无疑是正确的方法。我已经这样做了,除了从控制器传递的
isProfileComplete
声誉
信息外,更新工作正常。我也应该在那里创建一个新的用户对象吗?我不明白为什么isProfileComplete和Reputation属性是不同的。我想出来了。我在视图表单中没有任何字段保留这些值。
Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser 
    Dim _user = (From u In dc.Users 
        Where u.ID = user.ID 
        Select u).Single 
    With _user
        .Name = user.Name
        .Email = user.Email
    End With
End Sub