嗨,我正在创建一个 MVVM 应用程序,并且我将我的数据库链接到该项目,我在我的项目中添加了一个 WCF 服务,并且我创建了一个服务引用,因此我可以将我的应用程序中的新数据添加到我的数据库中。
在我的应用程序视图模型中,我有一个将新用户保存到数据库的命令,并在该 C# 文件中创建我的服务客户端的新实例,然后该命令存储在服务文件中。但是,当我使用我的服务调用此命令时,我收到错误消息:“XXX 的最佳重载方法匹配有一些无效参数”,我不知道为什么,因为所有内容似乎都已正确添加。
这是我的一些代码,向您展示我目前在视图模型中的内容,并且在这一行下我收到错误 (_serviceClient.SaveNewUser(newUser);) :
public RelayCommand AddUserCommand { get; private set; }
private ServiceClient _serviceClient;
public AddUserViewModel()
{
_serviceClient = new ServiceClient();
AddUserCommand = new RelayCommand(() => ExecuteAddUserCommand());
}
private void ExecuteAddUserCommand()
{
User newUser = new User();
newUser.FirstName = this.FirstName;
newUser.LastName = this.LastName;
newUser.ContactNumber = this.ContactNumber;
newUser.Email = this.Email;
//service call
int userID = _serviceClient.SaveNewUser(newUser);
newUser.UserID = userID;
}
然后在 service.svc.cs 我有:
[OperationContract]
public int SaveNewUser(User user)
{
using (var context = new SolutionEntities())
{
context.Users.AddObject(user);
context.SaveChanges();
return user.UserID;
}
}
用户类看起来像:
public partial class Client : EntityObject
{
public static Client CreateClient(global::System.Int32 userID, global::System.String firstName, global::System.String lastName, global::System.String contactNumber)
{
User user = new User();
user .UserID = userID;
user .FirstName = firstName;
user .LastName = lastName;
user .ContactNumber = contactNumber;
return user;
}
public global::System.Int32 UserID
{
get
{
return _UserID;
}
set
{
if (_UserID!= value)
{
OnUserIDChanging(value);
ReportPropertyChanging("UserID");
_UserID= StructuralObject.SetValidValue(value, "UserID");
ReportPropertyChanged("UserID");
OnUserIDChanged();
}
}
}
private global::System.Int32 _UserID;
partial void OnUserIDChanging(global::System.Int32 value);
partial void OnUserIDChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String FirstName
{
get
{
return _FirstName;
}
set
{
OnFirstNameChanging(value);
ReportPropertyChanging("FirstName");
_FirstName = StructuralObject.SetValidValue(value, false, "FirstName");
ReportPropertyChanged("FirstName");
OnFirstNameChanged();
}
}
private global::System.String _FirstName;
partial void OnFirstNameChanging(global::System.String value);
partial void OnFirstNameChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String LastName
{
get
{
return _LastName;
}
set
{
OnLastNameChanging(value);
ReportPropertyChanging("LastName");
_LastName = StructuralObject.SetValidValue(value, false, "LastName");
ReportPropertyChanged("LastName");
OnLastNameChanged();
}
}
private global::System.String _LastName;
partial void OnLastNameChanging(global::System.String value);
partial void OnLastNameChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String ContactNumber
{
get
{
return _ContactNumber;
}
set
{
OnContactNumberChanging(value);
ReportPropertyChanging("ContactNumber");
_ContactNumber = StructuralObject.SetValidValue(value, false, "ContactNumber");
ReportPropertyChanged("ContactNumber");
OnContactNumberChanged();
}
}
private global::System.String _ContactNumber;
partial void OnContactNumberChanging(global::System.String value);
partial void OnContactNumberChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String Email
{
get
{
return _Email;
}
set
{
OnEmailChanging(value);
ReportPropertyChanging("Email");
_Email = StructuralObject.SetValidValue(value, true, "Email");
ReportPropertyChanged("Email");
OnEmailChanged();
}
}
private global::System.String _Email;
partial void OnEmailChanging(global::System.String value);
partial void OnEmailChanged();
希望有人能看到我的问题所在,谢谢。