mardi 8 mars 2022

Accessing Owned properties with reflection

I'm struggling to dynamically expose a list of values containing Name, Database Column Name and value of an entity containing owned types.

public class Foo {
    public int Id { get; set; }
    public Certificate Certificate { get; set; }
}

[Owned]
public class Certificate {
    public string Number { get; set; }
    // Some logic here
}

public class PropBinding()
{
    public string Name{ get; set; }
    public string ColName{ get; set; }
    public string Content{ get; set; }
}

Owning relationship declared in model builder

modelBuilder.Entity<Foo>(entity =>
    entity.OwnsOne(e => e.Certificate)
          .UsePropertyAccessMode(PropertyAccessMode.Property)

With other declarations like column name. This information is stored in a single table, this reduced example with an int Id and a string Number.

Using Reflexion, I can get the values of Foo properties. Then I identify owned types and then call my function again to have the bindings off owned types properties, but it fails.

foreach(var propBinding in GetBindings(myFoo, myFoo.GetType())
{
    // no problem 
}

var owned = contentType.GetNavigations().FirstOrDefault(x => x.ForeignKey.IsOwnership);
foreach(var propBinding in GetBindings(myFoo, owned.ClrType())
{
    // success for name and colname
    // fails to get the value due to wrong type exception
}

public class IEnumerable<PropBinding> GetBindings(T entity, System.Type typeO) // Simplified for the sake of clarity
{
    var contentType = _db.Model.FindEntityType(entity.GetType());
    var storeObjectIdentifier = StoreObjectIdentifier.Table(contentType.GetTableName(), contentType.GetSchema());
    var props = typeO.GetProperties().Where(x => x.PropertyType.BaseType != null).ToList();
    foreach(var prop in props)
    {
        yield return new PropBinding() {
            Name = prop.Name,
            ColName = contentType.FindProperty(p.Name)?.GetColumnName(storeObjectIdentifier),
            Content = prop.GetValue(entity, null)
        }
    }
}

Thank you





Aucun commentaire:

Enregistrer un commentaire