我正在使用DataStax C#驱动程序及其Linq功能为Cassandra Db创建一个通用存储库。通用更新功能如下:
public interface ICassandraRepository<T>
{
Task<CrudResult> UpdateAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, T>> selector);
}
及其实现如下:
public class CassandraRepository<T> : ICassandraRepository<T>
{
protected readonly ISession _session;
protected readonly IMapper _mapper;
public async Task<bool> UpdateAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, T>> selector)
{
try
{
var table = new Table<T>(_session);
await table.Where(predicate).Select(selector).Update().ExecuteAsync();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
如果我有这样的实体类:
[Table("titles")]
public class Title
{
[PartitionKey()]
[Column("title_id")]
public Guid TitleId { get; set; }
[Column("title_name")]
public string TitleName { get; set; }
}
当我像这样调用此通用更新方法时,它会更新确定:
public async Task<bool> UpdateTitle(Title title)
{
private readonly ICassandraRepository<Title> _titleRepo;
return await _titleRepo.UpdateAsync(p => p.TitleId == title.TitleId,
s => new Title {TitleName = title.TitleName} );
}
但是当我按如下方式调用它时,出现异常“未为成员定义任何映射:标题”
public async Task<bool> UpdateTitle(Title title)
{
private readonly ICassandraRepository<Title> _titleRepo;
return await _titleRepo.UpdateAsync(p => p.TitleId ==
title.TitleId, s => title);
}