Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Azure Cosmos DB GetById查询不工作_.net_Mongodb_Azure_Azure Cosmosdb_Mongodb .net Driver - Fatal编程技术网

.net Azure Cosmos DB GetById查询不工作

.net Azure Cosmos DB GetById查询不工作,.net,mongodb,azure,azure-cosmosdb,mongodb-.net-driver,.net,Mongodb,Azure,Azure Cosmosdb,Mongodb .net Driver,我有一个应用程序目前正在使用Mongo DB。我希望将应用程序移动到Azure并尝试使用Cosmos DB。我将代码中的C#Mongo DB驱动程序升级到了最新版本2.7.0,并且在使用Mongo DB的情况下仍然可以正常工作 然后,我使用Cosmos DB迁移工具将数据迁移到Azure Cosmos DB Emulator,并将web配置中的连接字符串更改为指向Emulator。应用程序正在加载,一些参考数据将在我的第一个屏幕上返回,但下面的GetById查询不起作用 public v

我有一个应用程序目前正在使用Mongo DB。我希望将应用程序移动到Azure并尝试使用Cosmos DB。我将代码中的C#Mongo DB驱动程序升级到了最新版本2.7.0,并且在使用Mongo DB的情况下仍然可以正常工作

然后,我使用Cosmos DB迁移工具将数据迁移到Azure Cosmos DB Emulator,并将web配置中的连接字符串更改为指向Emulator。应用程序正在加载,一些参考数据将在我的第一个屏幕上返回,但下面的GetById查询不起作用

    public virtual T GetById(TKey id)
    {
        if (typeof(T).IsSubclassOf(typeof(EntityBase)))
        {
            return GetById(new ObjectId(id as string));
        }

        //code removed for brevity
    }

    public virtual T GetById(ObjectId id)
    {
        var filter = Builders<T>.Filter.Eq("_id", id);

        var result = collection.FindSync<T>(filter).FirstOrDefault();

        return result;
    }
这是在使用迁移数据工具进行迁移后,同一对象在Azure Cosmos DB Emulator中的外观

{
    "_id": "5b97a56b6381fecd00f0e10a",
    "LastUpdatedOn": [
        636722473812102500,
        -240
    ],
    "CreatedOn": [
        636722473396922500,
        -240
    ],
    "LastUpdatedBy": "SYSTEM",
    "CreatedBy": "TestUser",
    "VersionNumber": 3,
    "Name": "Audi",

它无法工作的原因可能是Id丢失了对象(“”)?我试图更新Azure Cosmos DB集合以添加该集合,但它给出了一个错误,表示值expected,就好像我没有指定正确的JSON格式一样。

从文档的CosmosDB emulator表示来看,您似乎需要更改GetById方法,以使用
字符串而不是
ObjectId

像这样的方法应该会奏效:

public virtual T GetById(TKey id)
{
    if (typeof(T).IsSubclassOf(typeof(EntityBase)))
    {
        return GetById(id as string);
    }

    //code removed for brevity
}

public virtual T GetById(string id)
{
    var filter = Builders<T>.Filter.Eq("_id", id);

    var result = collection.FindSync<T>(filter).FirstOrDefault();

    return result;
}
public虚拟T GetById(TKey id)
{
if(typeof(T).IsSubclassOf(typeof(EntityBase)))
{
返回GetById(id为字符串);
}
//为简洁起见,删除了代码
}
公共虚拟T GetById(字符串id)
{
var filter=Builders.filter.Eq(“\u id”,id);
var result=collection.FindSync(filter.FirstOrDefault();
返回结果;
}

这不起作用的实际原因是使用Azure Cosmos DB迁移也是为配合Cosmos DB SQL API而设计的。我想将MongoDB API作为Cosmos DB API的目标

将数据输入模拟器的方法是使用mongoimport和mongoexport exe,如下所述:


您是否尝试过将
\u id
作为
字符串而不是
ObjectId
添加到过滤器中?@NickChapsas-我没有没有-我会试试看-只是希望尽可能少地更改代码我希望它在没有更改的情况下工作,但emulator文档表示暗示了其他情况。试试看执行“重置数据”(右键单击仿真器托盘图标)@nickchapsa-有效:)干杯-它可以将数据检索到屏幕,但在尝试更新该数据时会抛出新错误-如果我无法解决,我将为此创建一个新问题
public virtual T GetById(TKey id)
{
    if (typeof(T).IsSubclassOf(typeof(EntityBase)))
    {
        return GetById(id as string);
    }

    //code removed for brevity
}

public virtual T GetById(string id)
{
    var filter = Builders<T>.Filter.Eq("_id", id);

    var result = collection.FindSync<T>(filter).FirstOrDefault();

    return result;
}