2013-11-15 80 views

回答

3

这将遍历数据库中的所有附件,并一次删除一个附件。

private void DeleteAllAttachments(IDocumentStore store) 
{ 
    while (true) 
    { 
     var header = store.DatabaseCommands 
      .GetAttachmentHeadersStartingWith("", 0, 1) 
      .FirstOrDefault(); 
     if (header == null) return; 
     store.DatabaseCommands.DeleteAttachment(header.Key, null); 
    } 
} 

您可以通过采用更大的页面大小来优化它,但由于您无论如何删除,它可能无关紧要。

1

据我所知,没有内置的选项来获取数据库的所有附件。但您可以执行以下操作:

var client = new HttpClient(); 
client.BaseAddress = new Uri("http://localhost:8080"); 
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); 
HttpResponseMessage response = client.GetAsync("databases/YourDatabaseName/static").Result; 
response.EnsureSuccessStatusCode(); 
var files = response.Content.ReadAsAsync<IList<AttachmentInformation>>().Result; 

foreach (var fileDetail in files) 
{ 
    store.DatabaseCommands.DeleteAttachment(fileDetail.Key, null); 
} 

要使用此功能,您需要以下POCO。并参考Microsoft.AspNet.WebApi.Client v5。如果您只是执行ReadAsString并自己或使用其他json解析器解析响应内容,则可以删除此要求。

public class AttachmentInformation 
{ 
    /// <summary> 
    ///  Gets or sets the size. 
    /// </summary> 
    /// <value>The size.</value> 
    public int Size { get; set; } 

    /// <summary> 
    ///  Gets or sets the key. 
    /// </summary> 
    /// <value>The key.</value> 
    public string Key { get; set; } 

    /// <summary> 
    ///  Gets or sets the metadata. 
    /// </summary> 
    /// <value>The metadata.</value> 
    public object Metadata { get; set; } 

    /// <summary> 
    ///  Gets or sets the etag. 
    /// </summary> 
    /// <value>The etag.</value> 
    public Guid Etag { get; set; } 
} 
+1

不错的答案,但是函数* does *存在于'GetAttachmentHeadersStartingWith'中,如果您希望它们全部可以使用空字符串。尽管如此,很高兴看到一些开箱即用的思想和HTTP API的使用。工作很好。 –