简体   繁体   English

尝试将不同的对象与 id 相关联(将对象保存到 ravendb 时)

[英]Attempted to associate a different object with id (when saving object into ravendb)

I have following code where I creating new object and trying to save into raven database.我有以下代码,我在其中创建新对象并尝试保存到 raven 数据库中。 ... ...

{
     foreach (var url in urls)
     {
        MyUrl u = new MyUrl {
        Id = url.Id,
        Url = url.Url
     };
     urlsCollection.Add(u);
     retrievedUrl = new RetrievedUrl {
        Id = url.Id,
        Url = url.Url
    };
    RavenSession.Store(retrievedUrl); //exception is thrown
    RavenSession.SaveChanges();
}

I'm getting我越来越

Raven.Client.Exceptions.NonUniqueObjectException Raven.Client.Exceptions.NonUniqueObjectException

on RavenSession.Store(retrievedUrl);RavenSession.Store(retrievedUrl);

with following exception message带有以下异常消息

An exception of type 'Raven.Client.Exceptions.NonUniqueObjectException' 
occurred in Raven.Client.Lightweight.dll but was not handled in user code

 Additional information: Attempted to associate a different object 
 with id 'RetrievedUrl/361'.

It seems that you are trying to save (at least) two different objects with the same Id and that is not possible.您似乎正在尝试保存(至少)两个具有相同 Id 的不同对象,这是不可能的。 RavenDB automatically creates unique ids when storing new documents, however, if you explicitly define the document Id, that will be used instead. RavenDB 在存储新文档时会自动创建唯一的 id,但是,如果您明确定义了文档 ID,则将使用该 ID。 Some more info in saving new document in raven . 在 raven保存新文档的更多信息。

This means you already have a different object in the session with the same id.这意味着您在会话中已经有一个具有相同 ID 的不同对象。 The fix for me was to use a new session.我的解决方法是使用新会话。 Alternatively you could copy over the values as per Update Document with external object或者,您可以根据使用外部对象的更新文档复制值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM