Is it possible through some combination of data annotations to map properties from a nested class to columns on the parent entity’s table?
For example:
class SubEntity {
public string SomeString { get; set; }
public string AnotherString { get; set; }
}
class Entity {
[PrimaryKey]
public Guid Id { get; set; }
public SubEntity Nested { get; set; }
}
Instead of blobbing the SubEntity object into a Nested column, I would like to tell OrmLite that the SomeString and AnotherString properties are actually distinct columns on the table for Entity. Is this possible? The main reason would be so I can group logically related columns together on a rather large entity representing a single table for code sanity purposes.
I’d highly recommend against using nested complex types and altering the structure of your code for grouping. Use partial classes if you want fields grouped into different classes but still retain the same code structure.
My 2 cents: Keep your DB objects in sync with your database structure. As @mythz pointed out, keep a 1 on 1 relation between DB entities and tables. One benefit of this is that when you query for the parent in the table, you will get only those fields returned.
Keep in mind that when you have non-null fields in your childs, you will run into issues upon inserting them via the parent if they do not have a default value.
Also, when there is a logical hierarchy between POCO’s, there may not be a real hierarchy on DB object (tables). This kind of defeats the purpose of layering your objects and could - for example - mean you need different tables to store the data. As an example (may not be the best):
An Animal base class may have Fish and Mammal as a child class. Your DB could the have tables for Animal, WalkingAnimal and SwimmingAnimal. This is cool, since some mammals can swim, which requires 2 joins.