我有以下代码调用DocumentDB并创建一个新的Employee文档.如何再将结果转换为Employee文档?基本上,我想捕获创建的文档并将其转换为Employee对象.
var result = await client.CreateDocumentAsync(collection.SelfLink, employee);
if(result.StatusCode == System.Net.HttpStatusCode.Created)
{
Employee emp = result.Resource; // How do I convert the result to Employee object here?
}
Run Code Online (Sandbox Code Playgroud) 考虑{ "userName": "user1" }存储在User集合中的文档,以及以下User类:
public class User
{
public string Id { get; set; }
public string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用以下JSON.net设置:
JsonConvert.DefaultSettings = () =>
{
return new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};
};
Run Code Online (Sandbox Code Playgroud)
当我用Linq查询时:
var t = _client.CreateDocumentQuery<User>(_collection.SelfLink)
.Where(u => u.UserName == "user1").AsDocumentQuery().ExecuteNextAsync();
t.Wait();
var users = t.Result;
var user = users.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
user一片空白.更改文档以使用pascal外壳或POCO使用骆驼外壳解决了这个问题.当然我不想要任何这些,因为我希望我的JSON对象和C#对象是"标准化的".
我如何告诉DocumentDB SDK使用驼峰套管映射我的对象的属性名称,类似于JSON.net?
如何从Cosmos DB中删除单个记录?
我可以选择使用SQL语法:
SELECT *
FROM collection1
WHERE (collection1._ts > 0)
Run Code Online (Sandbox Code Playgroud)
果然所有文件(类似于行?)都会被退回
但是,当我尝试删除时,这不起作用
DELETE
FROM collection1
WHERE (collection1._ts > 0)
Run Code Online (Sandbox Code Playgroud)
我如何实现这一目标?
从新的CosmosDb模拟器我得到了一个存储库来执行基本的documentdb操作,这个存储库被注入其他类.我想对基本查询进行单元测试.
public class DocumentDBRepository<T> where T : class
{
//Details ommited...
public IQueryable<T> GetQueryable()
{
return _client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true });
}
public async Task<IEnumerable<T>> ExecuteQueryAsync(IQueryable<T> query)
{
IDocumentQuery<T> documentQuery = query.AsDocumentQuery();
List<T> results = new List<T>();
while (documentQuery.HasMoreResults)
{
results.AddRange(await documentQuery.ExecuteNextAsync<T>());
}
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
此存储库需要一个文档客户端才能工作,它也会在构造函数上注入.
public DocumentDBRepository(string databaseId, IDocumentClient client)
{
_client = client;
_databaseId = databaseId;
_collectionId = GetCollectionName();
}
Run Code Online (Sandbox Code Playgroud)
我最初的方法是使用CosmosDb模拟器,但这需要模拟器运行我不喜欢并使测试更慢.
我的第二种方法是尝试使用文档客户端的模拟.
var data = new List<MyDocumentClass>
{ …Run Code Online (Sandbox Code Playgroud) 基于 PR:https ://github.com/terraform-providers/terraform-provider-azurerm/pull/5325
需要 azurerm 提供程序版本 1.42.0,以便默认安装服务器版本 3.6 的 cosmosdb 帐户。为了实现这一目标,我在模板中添加了以下内容,不幸的是,该模板失败并出现以下错误。
提供商“azurerm”{版本=“〜> 1.42.0”}
hashicorp/azurerm:没有可用的版本与给定的约束相匹配 1.35.0,〜> 1.35.0,〜> 1.42.0,〜> 1.35.0 无法检索提供程序的可用版本列表
有人可以阐明这一点吗?我在这里缺少什么
我看到一些奇怪的行为保存到DocumentDB.我开始使用看起来像这样的普通旧类来保存文档:
public class Person
{
public string Name;
public int Age;
}
Run Code Online (Sandbox Code Playgroud)
我保存这些文件是这样的:
var person = new Person { ... };
client.CreateDocumentAsync(myCollectionLink, person);
Run Code Online (Sandbox Code Playgroud)
这很好.保存的属性与类中的名称完全相同.然后我意识到我需要文档的SelfLink才能执行更新和删除."啊,"我想."我只是从Document中衍生出来,就像这样:
public class Person: Microsoft.Azure.Documents.Document
{
public string Name;
public int Age;
}
Run Code Online (Sandbox Code Playgroud)
然而,令我惊讶的是,当我进行此更改时,除了DocumentDB本身分配的"id"属性外,新文档创建完全空白.
我多次仔细检查.从文档派生可防止保存文档中的自定义属性...
...除非我用[JsonProperty]明确地装饰每一个,如下所示:
public class Person: Document
{
[JsonProperty(PropertyName="name")]
public string Name;
[JsonProperty(PropertyName="age")]
public int Age;
}
Run Code Online (Sandbox Code Playgroud)
然后它再次工作(当然,使用新的更多JSON适当的camelCase属性名称).并且,在检索时,对象将填充我需要更新和删除的SelfLink属性.都好.
我的问题是...... 为什么会这样?我是从文件衍生出来做错了吗?非常感谢您的反馈意见.
是否可以在DocumnetDb上执行不区分大小写的搜索?
假设我有一个'name'键和值为"Timbaktu"的记录
这将有效:
select * from json j where j.name = "Timbaktu"
Run Code Online (Sandbox Code Playgroud)
这不会:
select * from json j where j.name = "timbaktu"
Run Code Online (Sandbox Code Playgroud)
那你怎么做一个不区分大小写的搜索呢?
提前致谢.
问候.
通常,我会启动 Azure Cosmos DB Emulator.exe(安装在 Windows 10 上),它会在 https://localhost:8081/_explorer/index.htm 启动 Chrome,并显示“恭喜!您的 Azure Cosmos DB 模拟器正在运行。”,很简单豌豆。但今天我遇到了一个问题,导致 Chrome 说 SSL 证书无效,Firefox 也是如此。
为了重新开始,我尝试了以下操作:
certlm.msc删除所有名为“localhost”的证书AppData\Local\CosmosDBEmulator同样的情况发生。每次我重新安装并运行模拟器时,它都会在幕后添加几个“本地主机”证书(如下),但根据浏览器的说法,它们似乎不受信任。根据名称,“受信任的根证书颁发机构”中的证书似乎与“个人”中的证书相匹配。这些必须是浏览器抱怨的自签名证书?(如果我错了,请纠正我,我不是这方面的专家)。我还尝试了一个通过导出/导入证书来获取受信任证书的视频,但这也没有任何作用。
我是否可以让我的浏览器将这些证书视为有效?只是希望它像以前一样工作 - 运行模拟器并让 localhost 在任何浏览器上工作,而无需弄乱证书或任何浏览器设置。任何帮助表示赞赏!
尝试检索单个记录时,我在尝试查询Azure DocumentDb存储帐户时遇到问题.这是我的WebAPI代码:
// Controller...
public AccountController : ApiController {
// other actions...
[HttpGet]
[Route("Profile")]
public HttpResponseMessage Profile()
{
var userId = User.Identity.GetUserId();
var rep = new DocumentRepository<UserDetail>();
var profile = rep.FindById(userId);
if (profile == null)
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Profile not found");
return Request.CreateResponse(HttpStatusCode.OK, profile);
}
}
// Repository
public class DocumentRepository<TEntity> : IDocumentRepository<TEntity> where TEntity : IIdentifiableEntity
{
private static DocumentClient _client;
private static string _databaseName;
private static string _documentsLink;
private static string _selfLink;
public DocumentRepository()
{
_client = new DocumentClient(new …Run Code Online (Sandbox Code Playgroud) 我正在使用Microsoft的一个名为DocumentDB的新数据库.现在我想通过ID删除文档,但我无法弄清楚,如何做到这一点.DocumentDB中的删除操作需要自我链接,它们与我自己的ID不同.但是我一次查询文档,然后我会得到自我链接.通过该自我链接,我将删除该文档.
现在我要删除我的收藏中大约50000多个文档的所有文档.
需要获取每个文档然后删除或任何简单的方法来做同样的事情?
有可能吗?
azure-cosmosdb ×10
azure ×5
c# ×4
certificate ×1
json ×1
linq ×1
localhost ×1
moq ×1
unit-testing ×1