PocoDynamo保存子对象时未正确应用Alias/DynamoDbProperty属性命名问题
I’ve run into this exact issue before with PocoDynamo—when dealing with nested collections, the [Alias] or [DynamoDbProperty] attributes on child objects don’t get respected during the save operation, even though they work fine when reading data. Let’s break down why this happens and how to fix it:
The Problem Recap
Your Practice parent object correctly uses the alias "docs" for its collection, but the child Doc objects inside that collection are still being saved with PascalCase property names (like Id instead of id). This mismatch causes the properties to come back as null/default values when you read the Practice later, even though the alias rules work during deserialization.
Why This Happens
PocoDynamo relies on ServiceStack.Text for serialization, and by default, it doesn’t always recursively apply alias attributes to nested objects/collections. You need to either explicitly configure the mappings or adjust the global serialization settings to ensure aliases are respected at every level.
Solutions to Try
1. Enable Alias Support in Global Serialization Config
First, make sure your ServiceStack.Text global config is set up to recognize the alias attributes. Add this code when your app starts (e.g., in Startup.cs or your initialization routine):
// For [Alias] attribute JsConfig.AliasAttributeType = typeof(AliasAttribute); // Optional: Turn off type info if you don't need it (avoids extra "__type" fields) JsConfig.IncludeTypeInfo = false;
If you’re using [DynamoDbProperty] instead, swap the attribute type:
JsConfig.AliasAttributeType = typeof(DynamoDbPropertyAttribute);
2. Explicitly Register Child Object Mappings with PocoDynamo
If global config doesn’t do the trick, you can manually register the property aliases for your Doc class when setting up PocoDynamo. This ensures the mapper knows exactly how to handle nested Doc objects:
var db = new PocoDynamo(yourDynamoDbClient); // Register parent table first db.RegisterTable<Practice>(); // Explicitly define aliases for the child Doc class db.RegisterTable<Doc>(table => { table.Property(doc => doc.Id).Alias("id"); table.Property(doc => doc.UserId).Alias("userId"); table.Property(doc => doc.Specialty).Alias("specialty"); });
3. Switch to DataContract/DataMember Attributes
If the above methods still don’t work, try using [DataContract] and [DataMember(Name = "...")] instead. ServiceStack.Text has robust support for these attributes, and PocoDynamo will honor them for nested objects without extra configuration:
[DataContract] public class Doc { [DataMember(Name = "id")] public Guid Id { get; set; } [References(typeof(User))] [DataMember(Name = "userId")] public Guid UserId { get; set; } [DataMember(Name = "specialty")] public string Specialty { get; set; } } [DataContract] public class Practice { public Practice() { Docs = new List<Doc>(); } [DataMember(Name = "id")] public Guid Id { get; set; } [Reference] [DataMember(Name = "docs")] public List<Doc> Docs { get; set; } }
How to Verify the Fix
- Save a new
Practiceobject to DynamoDB after applying one of the fixes - Check the DynamoDB console to confirm the nested
docsitems use your specified aliases (e.g.,idinstead ofId) - Fetch the
Practiceby ID again—yourDocproperties should now load correctly instead of being null/default.
内容的提问来源于stack exchange,提问作者DDiVita

