Search This Blog

Wednesday, December 30, 2015

EF - Use of complex types that are not registered in DbContext and have no key property

I want to highlight a minor usability improvement built into v15.2 for our Entity Framework fans. This is all about a simpler integration of complex types without keys (learn more from EF docs...). 

The situation is already pretty much summarized in the Support Center, and here I just want to re-post the main details. To begin with, consider the following EF classes:

[C#]
public class Product { public Product() { PriceRange = new Range(); } public Int32 ID { get; set; } public String Name { get; set; } public Range PriceRange { get; set; } } public class Range { public Range() { } public Decimal Low { get; set; } public Decimal High { get; set; } }
public class EFDemoDbContext : DbContext {
    ...
    public DbSet<Product> Products { get; set; }
    ...
}

Note that the Range class has no key property and Entity Framework does not map this class to a separate database table. By default, properties of such a class persist in the table mapped to the parent Product class, where the Range type property is declared. So, the Product table will have the ID, NamePriceRange_Low and PriceRange_High columns:



Starting with v15.2.4 we recommend that you apply the ExpandObjectMembers attribute to properties of a complex type:
[C#]
[ExpandObjectMembers(ExpandObjectMembers.Always)] public Range PriceRange { get; set; }
to be able to view and edit them in the XAF CRUD UI without any additional steps:


Should you require support for other use-case scenarios involving complex types, please let me know via the Support Center or here in comments. Thanks!

P.S.
Happy holidays!

No comments:

Post a Comment