2010-02-16 28 views
19

Nếu tôi có một lớp học như thế này:Làm thế nào để bạn loại trừ một thuộc tính khỏi bị lưu giữ trong bộ nhớ Azure Table?

public class Facet : TableServiceEntity 
{ 
    public Guid ParentId { get; set; }  
    public string Name { get; set; } 
    public string Uri{ get; set; } 
    public Facet Parent { get; set; } 
} 

Chánh có nguồn gốc từ ParentId Guid, và mối quan hệ đó được thiết kế để được điền vào bằng kho của tôi. Vậy làm cách nào để tôi bảo Azure rời khỏi trường đó một mình? Có một thuộc tính Bỏ qua nào đó của một loại nào đó hay tôi phải tạo một lớp thừa kế để cung cấp các mối quan hệ đó thay thế?

+0

Họ làm gì bây giờ http://stackoverflow.com/questions/5379393/do-azure-table -services-entity-have-an-equivalent-of-nonserializedattribute –

Trả lời

4

Câu trả lời này từ Andy Cross tại bwc --- Cảm ơn bạn lần nữa Andy. This question an azure forums

Hi,

Sử dụng WritingEntity và các sự kiện ReadingEntity. http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.writingentity.aspx Điều này cung cấp cho bạn tất cả các điều khiển bạn cần.

Để tham khảo có một bài đăng blog liên kết tắt ở đây quá: http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/d9144bb5-d8bb-4e42-a478-58addebfc3c8

Cảm ơn Andy

+2

Đáng buồn là các liên kết đến diễn đàn không hoạt động nữa :-(MSDN thực sự làm hỏng các liên kết của nó! –

3

Bạn có thể ghi đè lên các phương pháp WriteEntity trong TableEntity và loại bỏ bất kỳ thuộc tính có thuộc tính tùy chỉnh của bạn.

public class CustomTableEntity : TableEntity 
{ 
    public override IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext) 
    { 
     var entityProperties = base.WriteEntity(operationContext); 
     var objectProperties = GetType().GetProperties(); 

     foreach (var property in from property in objectProperties 
           let nonSerializedAttributes = property.GetCustomAttributes(typeof(NonSerializedOnAzureAttribute), false) 
           where nonSerializedAttributes.Length > 0 
           select property) 
     { 
      entityProperties.Remove(property.Name); 
     } 

     return entityProperties; 
    } 
} 

[AttributeUsage(AttributeTargets.Property)] 
public class NonSerializedOnAzureAttribute : Attribute 
{ 
} 

sử dụng

public class MyEntity : CustomTableEntity 
{ 
    public string MyProperty { get; set; } 

    [NonSerializedOnAzure] 
    public string MyIgnoredProperty { get; set; } 
} 
8

Có một thuộc tính gọi là WindowsAzure.Table.Attributes.IgnoreAttribute thể được đặt trên tài sản bạn muốn loại trừ. Chỉ cần sử dụng:

[Ignore] 
public string MyProperty { get; set; } 

Nó là một phần của Windows Azure Extensions lưu trữ, mà bạn có thể tải về từ: https://github.com/dtretyakov/WindowsAzure

hoặc cài đặt như là một gói: https://www.nuget.org/packages/WindowsAzure.StorageExtensions/

Thư viện này được MIT cấp phép.

+3

Điều này đã được thay thế bởi 'IgnorePropertyAttribute', xem [IgnorePropertyAttribute Lớp] (https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.ignorepropertyattribute.aspx). – Aaron

15

Sử dụng mới nhất Microsoft.WindowsAzure.Storage SDK (v6.2.0 trở lên), tên thuộc tính đã thay đổi để IgnorePropertyAttribute:

public class MyEntity : TableEntity 
{ 
    public string MyProperty { get; set; } 

    [IgnoreProperty] 
    public string MyIgnoredProperty { get; set; } 
} 
Các vấn đề liên quan