Skip to main content
 首页 » 编程设计

c#之WCF SQL 插入 GUID 转换错误

2024年02月23日17haluo1

我不确定该怎么做,

我想使用 WCF 为我的用户添加唯一标识符,当我向我的客户数据上下文添加 GUID 时,它会抛出此错误

Error 2
Argument 1: cannot convert from 'System.Guid' to 'ServiceFairy.client' C:\Users\John\Documents\Visual Studio 2010\Projects\PremLeague\ServiceFairy\Service1.svc.cs

有什么机会可以帮忙吗?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 
using System.Data.SqlClient; 
using System.Diagnostics; 
 
namespace ServiceFairy 
{ 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class     name "Service1" in code, svc and config file together. 
public class Service1 : IService1 
{ 
    public List<match> GetAllMatches() 
    { 
        matchtableDataContext context = new matchtableDataContext(); 
        var matches = from m in context.matches orderby m.Date select m; 
        return matches.Take(100).ToList(); 
    } 
 
    public List<premtable> GetTable() 
    { 
        premContext context = new premContext(); 
        var table = from user in context.premtables orderby user.ID select   user; 
        return table.Take(100).ToList(); 
    } 
 
    public Dictionary<Guid, Uri> _clientUris = new Dictionary<Guid, Uri>(); 
 
    public void Subscribe(Guid clientID, string uri) 
    { 
        ClientsDBDataContext context = new ClientsDBDataContext(); 
        context.clients.Insert(clientID);            
    }        

请您参考如下方法:

问题出在您的 Subscribe 方法中的这一行:

context.clients.Insert(clientID);   // error: clientID is the wrong type 

您将 GUID 类型的 clientID 传递给 Insert() 而您应该传递 ServiceFairy.client 类型的对象。

看起来您应该创建一个新的 client 对象并保存它:

var client = new ServiceFairy.client() { ClientID = clientID };  // TODO: set other properties 
context.clients.Insert(client); 

如 TODO 所示,您还应该设置其他 client 属性。