2012-10-14 46 views
0

我想存储一个名为Tshirt的实体到Windows Azure表存储以及Windows Azure Blob存储上的Blob。 该实体Tshirt包含一个名为Image(byte [])的字段,但我不想将其保存在我的表中。 我如何在我的班级中表明我不想保存该字段?确定哪些字段要保存在Windows Azure表存储

public class Tshirt : TableServiceEntity 
{ 

    public Tshirt(string partitionKey, string rowKey, string name) 
    { 
     this.PartitionKey = partitionKey; 
     this.RowKey = rowKey; 
     this.Name = name; 

     this.ImageName = new Guid(); 
    } 

    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 


    private string _color { get; set; } 

    public string Color 
    { 
     get { return _color; } 
     set { _color = value; } 
    } 


    private int _amount { get; set; } 

    public int Amount 
    { 
     get { return _amount; } 
     set { _amount = value; } 
    } 


    [NonSerialized] 
    private byte[] _image; 


    public byte[] Image 
    { 
     get { return _image; } 
     set { _image = value; } 
    } 


    private Guid _imageName; 

    public Guid ImageName 
    { 
     get { return _imageName; } 
     set { _imageName = value; } 
    } 
} 
+0

请帮我http://stackoverflow.com/questions/14156980/save-to-windows-azure-table – Ladessa

+0

对于SDK 2.0版本,这里是一个答案的http:// stackoverflow.com/a/15179860/828957 – Rob

回答

2

最简单的办法是揭露场的一对方法,而不是实际属性:

public byte[] GetImage() 
{ 
    return _image; 
} 

public void SetImage(byte[] image) 
{ 
    _image = image; 
} 

如果这不是一个选项,然后当你可以删除图片属性通过处理WriteEntity事件来存储实体。 (Credit to Neil Mackenzie

public void AddTshirt(Tshirt tshirt) 
{ 
    var context = new TableServiceContext(_baseAddress, _credentials); 
    context.WritingEntity += new EventHandler<ReadingWritingEntityEventArgs>(RemoveImage); 
    context.AddObject("Tshirt", tshirt); 
    context.SaveChanges(); 
} 

private void RemoveImage(object sender, ReadingWritingEntityEventArgs args) 
{ 
    XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; 
    XElement imageElement = args.Data.Descendants(d + "Image").First(); 
    imageElement.Remove(); 
} 
+0

http://stackoverflow.com/questions/14156980/save-to-windows-azure-table – Ladessa