0

我正在尝试将名为 Tshirt 的实体与 Windows Azure Blob 存储上的 Blob 一起存储到 Windows Azure 表存储中。该实体 Tshirt 包含一个名为 Image (byte[]) 的字段,但我不想将其保存在我的表中。如何在课堂上表明我不想保存该字段?

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; }
    }
}
4

1 回答 1

2

简单的方法是将字段公开为一对方法而不是实际属性:

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

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

如果这不是一个选项,那么您可以在存储实体时通过处理 WritingEntity 事件来删除 Image 属性。(归功于尼尔麦肯齐

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();
}
于 2012-10-15T18:43:43.247 回答