簡體   English   中英

不支持MongoDB Serialization()

[英]MongoDB Serialization() is not supported

不知道我在做什么,也找不到任何其他關於這個錯誤甚至是什么相關的參考。

我收到錯誤消息Serialization().Reference is not supported當我使用MondoDB和C#.NET嘗試下面的代碼時, Serialization().Reference is not supported

我在我的代碼中首先嘗試了這個:

var maxReference = await Events.Find(p => true)
                               .SortByDescending(p => p.Reference)
                               .Project(p => p.Reference)
                               .FirstOrDefault();

FirstOrDefault方法失敗時,我逐漸刪除方法,直到我不得不這樣做:

var list = await Events.Find(p => true).ToListAsync();
var maxReference = list.Select(p => p.Reference)
                       .OrderByDescending(p => p)
                       .FirstOrDefault();

如果它是一個真正的限制,我願意使用它,但當我嘗試使用ReplaceOneAsync方法時,我又遇到了它。

var form = new Event { Reference = maxReference + 1 };
var options = new UpdateOptions { IsUpsert = true };

await Events.ReplaceOneAsync(p => p.Reference == maxReference, form, options);

我的POCO定義為:

[BsonIgnoreExtraElements]
public class Event : IEvent
{
    public Event() {}

    public Event(int reference)
    {
        Reference = reference;
    }

    [BsonId]
    public ObjectId EventID { get; set; }

    [BsonRequired]
    [BsonIgnoreIfDefault]
    public int Reference { get; set; }
}

所以事實證明,在我的情況下,在依賴注入中我使用了接口而且不支持。 我想這與Mongo Documents的實例化有關? 下面是最終的bootstrapper類。 所以它似乎最終不像Nancy問題那樣是Mongo問題。

我改變了行var collection = database.GetCollection<ICollection>("collection"); to var collection = database.GetCollection<Collection>("collection");

注意Collection類的ICollection接口。 之后一切順利。

之前:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        var connectionString = ;
        var databaseName = ;

        var client = new MongoClient("MongoDatabaseURL");
        container.Register(client);

        var database = client.GetDatabase("MongoDatabaseName");
        container.Register(database);

        var collection = database.GetCollection<ICollection>("collection");
        container.Register(collection);
    }
}

后:

public class Bootstrapper : DefaultNancyBootstrapper {
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        var connectionString = ;
        var databaseName = ;

        var client = new MongoClient("MongoDatabaseURL");
        container.Register(client);

        var database = client.GetDatabase("MongoDatabaseName");
        container.Register(database);

        var collection = database.GetCollection<Collection>("collection");
        container.Register(collection);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM