C# 用c更新mongoDB中的查询
x变量中的编译器错误CS1061,我想更新mongodb中的查询,但问题是引发x错误C# 用c更新mongoDB中的查询,c#,asp.net,mongodb,core,C#,Asp.net,Mongodb,Core,x变量中的编译器错误CS1061,我想更新mongodb中的查询,但问题是引发x错误 public async Task<string> Update(string id, TEntity user) { await collection.ReplaceOneAsync(x => x.id == id, user); return ""; } 在这段代码中:ReplaceOneAsyncx=>x.id==id,用户x的类型是TE
public async Task<string> Update(string id, TEntity user)
{
await collection.ReplaceOneAsync(x => x.id == id, user);
return "";
}
在这段代码中:ReplaceOneAsyncx=>x.id==id,用户x的类型是TEntity
错误表明,从编译器的角度来看,TEntity不包含属性id
解决这个问题的一种方法是定义一个抽象概念,每个抽象概念都必须继承自:
公共界面的开放性
{
字符串id{get;set;}
}
然后根据您发布的方法,在repository类中,我假设它是TEntity的通用存储库类,在TEntity上添加通用约束,如下所示:
公共类MyRepository,其中tenty:ienty
{
//集合应该是一个集合
私有IMongoCollection集合;//在别处初始化
公共异步任务更新id,TEntity用户
{
wait collection.ReplaceOneAsyncx=>x.id==id,用户;
回来
}
//……其他成员。。。
}
因为我们包含了通用约束,其中tenty:ienty,编译器现在知道每个tenty都有一个字符串id属性。异常的完整消息是什么?Erreur CS1061'tenty'ne contient pas de dédefinition pour'id'et aucune méthode d d'extension accessible'id'acceptance un premier argument de type'tenty'n'aététrouv e un指令使用您自己的语言那么什么类型的集合呢?集合的元素内部是否有id属性?错误声明它不存在