我正嘗試使用LINQ to SQL更新我的Students表記錄。問題是當對象的其中一個屬性發生變化時,'PropertyChanging'使用'SendPropertyChanging()'方法觸發事件。LINQ to SQL更新問題
請注意:
- student_id數據是PK
- 數據類型匹配
這裏是完全的ArgumentException:
GenericArguments [2],「系統。字符串',在 'System.Data.Linq.Mapping.PropertyAccessor + Accessor`3 [T,V,V2]' 違反了'V2'類型的約束。
堆棧strace的
在System.RuntimeType.ValidateGenericArguments(的MemberInfo 定義,RuntimeType [] genericArguments,例外五)在 System.RuntimeType.MakeGenericType在 系統(類型[]實例化)。 Data.Linq.Mapping.PropertyAccessor.Create(Type objectType, PropertyInfo pi,MetaAccessor storageAccessor)at System.Data.Linq.Mapping.AttributedMetaDataMember.MakeMemberAccessor(Type accessorType,MemberInfo mi,MetaAccessor storage)at 個System.Data.Linq.Mapping.AttributedMetaDataMember.InitAccessors()
在 System.Data.Linq.Mapping.AttributedMetaDataMember.get_StorageAccessor() 在 System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.CreateDataCopy(對象 實例)處 pro.Student.SendPropertyChanging() System.Data.Linq.ChangeTracker.StandardChangeTracker.OnPropertyChanging(對象 發件人,PropertyChangingEventArgs參數)在 C:\的Inetpub \ wwwroot的\親\模型\ Student.vb:線384 at pro.Student.set_first_name(String value)in C:\ inetpub \ wwwroot \ pro \ Models \ Student.vb:line 132 at pro .WizardController.SetLanguageAndText(LanguageAndTextInfo ltInfo) C:\ inetpub \ wwwroot \ pro \ Controllers \ WizardController.vb:line 517 at lambda_method(Closure,ControllerBase,Object [])at System.Web.Mvc.ActionMethodDispatcher.Execute在 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerBase 控制器,對象[]參數)(ControllerContext controllerContext,IDictionary的參數)在 System.Web.Mvc.ControllerActionInvoker。 <> C_ DisplayClass15.b _12() 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter 濾波器,ActionExecutingContext preContext,Func`1續)
這裏是學生。VB類:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Imports System.IO
Imports System.Data.Linq.Mapping
Imports System.ComponentModel
<Global.System.Data.Linq.Mapping.TableAttribute(name:="dbo.Students")> _
Public Class Student
Implements System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
Private Shared emptyChangingEventArgs As PropertyChangingEventArgs = New PropertyChangingEventArgs(String.Empty)
Private _student_id As System.Guid
Private _first_name As String
Private _last_name As String
Private _email As String
Public Sub New()
End Sub
#Region "Properties"
<Global.System.Data.Linq.Mapping.ColumnAttribute(name:="student_id", Storage:="_student_id", DbType:="uniqueidentifier NOT NULL", IsPrimaryKey:=True, IsDbGenerated:=True, CanBeNull:=False)> _
Public Property student_id() As System.Guid
Get
Return _student_id
End Get
Set(value As System.Guid)
If ((_student_id = value) = False) Then
SendPropertyChanging()
_student_id = value
SendPropertyChanged("student_id")
End If
End Set
End Property
<Global.System.Data.Linq.Mapping.ColumnAttribute(name:="first_name", Storage:="_first_name", DbType:="nvarchar(250) NOT NULL", CanBeNull:=False)> _
Public Property first_name() As String
Get
Return _first_name
End Get
Set(ByVal value As String)
If ((_first_name = value) = False) Then
**SendPropertyChanging()**
_first_name = value
SendPropertyChanged("first_name")
End If
End Set
End Property
<Global.System.Data.Linq.Mapping.ColumnAttribute(name:="last_name", Storage:="_last_name", DbType:="nvarchar(250) NOT NULL", CanBeNull:=False)> _
Public Property language() As String
Get
Return _last_name
End Get
Set(ByVal value As String)
If ((_last_name = value) = False) Then
**SendPropertyChanging()**
_last_name = value
SendPropertyChanged("last_name")
End If
End Set
End Property
<Global.System.Data.Linq.Mapping.ColumnAttribute(name:="email", Storage:="_email", DbType:="nvarchar(250) NOT NULL", CanBeNull:=False)> _
Public Property email() As String
Get
Return _email
End Get
Set(ByVal value As String)
If ((_email = value) = False) Then
**SendPropertyChanging()**
_email = value
SendPropertyChanged("email")
End If
End Set
End Property
#End Region
#Region "Linq Methods"
Public Event PropertyChanging As PropertyChangingEventHandler Implements System.ComponentModel.INotifyPropertyChanging.PropertyChanging
Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub SendPropertyChanging()
If ((Me.PropertyChangingEvent Is Nothing) = False) Then
RaiseEvent PropertyChanging(Me, emptyChangingEventArgs)
End If
End Sub
Protected Overridable Sub SendPropertyChanged(ByVal propertyName As [String])
If ((Me.PropertyChangedEvent Is Nothing) = False) Then
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
#End Region
End Class
這是我正在努力更新學生
Using context As New dbStudents(strConnString)
Dim tmpStudent = (From p In context.Students
Where p.student_id = CurStudent.student_id
Select p).FirstOrDefault()
tmpStudent.FirstName = CurStudent.FirstName
tmpStudent.LastName = CurStudent.LastName
tmpStudent.Email = CurStudent.Email
context.SubmitChanges()
End Using
謝謝!
不幸運!在'System.Data.Linq.Mapping.PropertyAccessor + Accessor'3 [T,V,V2]上獲得相同的ArgumentException:{「GenericArguments [2],'System.String''違反了'V2'類型的約束。 「} –