private static void GetManyToOneChild <T>(this SQLiteConnection conn, ref T element, PropertyInfo relationshipProperty) { var type = typeof(T); EnclosedType enclosedType; var entityType = relationshipProperty.GetEntityType(out enclosedType); Debug.Assert(enclosedType == EnclosedType.None, "ManyToOne relationship cannot be of type List or Array"); var otherEntityPrimaryKeyProperty = entityType.GetPrimaryKey(); Debug.Assert(otherEntityPrimaryKeyProperty != null, "ManyToOne relationship destination must have Primary Key"); var currentEntityForeignKeyProperty = type.GetForeignKeyProperty(relationshipProperty); Debug.Assert(currentEntityForeignKeyProperty != null, "ManyToOne relationship origin must have Foreign Key"); var tableMapping = conn.GetMapping(entityType); Debug.Assert(tableMapping != null, "There's no mapping table for OneToMany relationship destination"); object value = null; var foreignKeyValue = currentEntityForeignKeyProperty.GetValue(element, null); if (foreignKeyValue != null) { value = conn.Find(foreignKeyValue, tableMapping); if (IsSoftDeleted(entityType, value)) { value = null; } } relationshipProperty.SetValue(element, value, null); }
private static void GetOneToOneChild <T>(this SQLiteConnection conn, ref T element, PropertyInfo relationshipProperty) { var type = typeof(T); EnclosedType enclosedType; var entityType = relationshipProperty.GetEntityType(out enclosedType); Debug.Assert(enclosedType == EnclosedType.None, "OneToOne relationship cannot be of type List or Array"); var currentEntityPrimaryKeyProperty = type.GetPrimaryKey(); var otherEntityPrimaryKeyProperty = entityType.GetPrimaryKey(); Debug.Assert(currentEntityPrimaryKeyProperty != null || otherEntityPrimaryKeyProperty != null, "At least one entity in a OneToOne relationship must have Primary Key"); var currentEntityForeignKeyProperty = type.GetForeignKeyProperty(relationshipProperty); var otherEntityForeignKeyProperty = type.GetForeignKeyProperty(relationshipProperty, inverse: true); Debug.Assert(currentEntityForeignKeyProperty != null || otherEntityForeignKeyProperty != null, "At least one entity in a OneToOne relationship must have Foreign Key"); var hasForeignKey = otherEntityPrimaryKeyProperty != null && currentEntityForeignKeyProperty != null; var hasInverseForeignKey = currentEntityPrimaryKeyProperty != null && otherEntityForeignKeyProperty != null; Debug.Assert(hasForeignKey || hasInverseForeignKey, "Missing either ForeignKey or PrimaryKey for a complete OneToOne relationship"); var tableMapping = conn.GetMapping(entityType); Debug.Assert(tableMapping != null, "There's no mapping table for OneToOne relationship"); var inverseProperty = type.GetInverseProperty(relationshipProperty); object value = null; if (hasForeignKey) { var foreignKeyValue = currentEntityForeignKeyProperty.GetValue(element, null); if (foreignKeyValue != null) { value = conn.Find(foreignKeyValue, tableMapping); } } else { var primaryKeyValue = currentEntityPrimaryKeyProperty.GetValue(element, null); if (primaryKeyValue != null) { var query = string.Format("SELECT * FROM [{0}] WHERE [{1}] = ? LIMIT 1", entityType.Name, otherEntityForeignKeyProperty.Name); value = conn.Query(tableMapping, query, primaryKeyValue).FirstOrDefault(); // Its a OneToOne, take only the first } } relationshipProperty.SetValue(element, value, null); if (value != null && inverseProperty != null) { inverseProperty.SetValue(value, element, null); } }