简体   繁体   English

指定的方法不支持保存mongodb集合

[英]The specified method is not supported saving mongodb collection

I'm working in a ASP MVC + Mongodb App, so I want to save files inside a specific document through a List. 我正在使用ASP MVC + Mongodb App,因此我想通过列表将文件保存在特定文档中。 I'm using GridFs Upload Method which returns a MongoGridFSFileInfo (The type of the List in my Model) so I get the reference to the object returned and Add it to the List --so far everything seems fine-- but when I try to save the changes in the collection I get this exception "The specified method is not supported" and the File won't save into the document. 我正在使用GridFs Upload方法,该方法返回MongoGridFSFileInfo (模型中列表的类型),因此我获得对返回对象的引用并将其添加到列表中-到目前为止一切似乎还不错-但当我尝试将更改保存到集合中时,出现此异常“不支持指定的方法”,并且文件不会保存到文档中。

This is my Model 这是我的模特

public class Document
    {

        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
        public string Name { get; set; }
        public string  Description { get; set; }
        public List<MongoGridFSFileInfo> FsFileInfos = new List<MongoGridFSFileInfo>(); 

        public Document()
        {

        }

    }

And Here is the Method where 1) Add a File to a List of Document Collection 2) Try to save the changes in the collection. 这是1)将文件添加到文档集合列表中的方法2)尝试将更改保存在集合中。

[HttpPost]
        public ActionResult PrepareToAttach(string id, string description, HttpPostedFileBase raw_file)
        {
            Document doc = Context.FindById(id);

            var options = new MongoGridFSCreateOptions
            {
                ContentType = raw_file.ContentType
            }; 
            var mongoFileInfo = Context._db.GridFS.Upload(raw_file.InputStream, raw_file.FileName,options);

            doc.FsFileInfos.Add(mongoFileInfo);
            Context.Documents.Save(doc);

            return RedirectToAction("Index", "Document");
        }

The exception is displayed in Context.Documents.Save(doc); 异常显示在Context.Documents.Save(doc);中。 --> The specified method is not supported -> 不支持指定的方法

MongoGridFSFileInfo is not a POCO class. MongoGridFSFileInfo不是POCO类。 What I mean by that is that it has many properties on it that either aren't persistable or shouldn't be. 我的意思是说,它具有许多不持久或不应该持久的属性。

The only thing you actually need to persist to get back a document is the unique identifier for the file. 您真正需要持久保存以取回文档的唯一东西是文件的唯一标识符。 You may want to persist some other meta-data as well, but only the identifier is required. 您可能还想保留其他一些元数据,但是仅需要标识符。

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

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