-
Notifications
You must be signed in to change notification settings - Fork 22
Collections
A collection type is an enumerable of entities or primitives that should be mapped item by item.
All collections are .NET Enumerables of other types but not all enumerables are necessarily collections. For example, string
is an enumerable of chars, but for mapping purposes it is configured as a primitive.
For collections of entities, a merge by key is performed. Target items are copied to an internal hashmap of Id -> Entity, then source is iterated and decision is performed based on whether the entity is present in both or only in source or target.
There are no specific configuration for Collections. Working as members, they are often associations so same configuration as references can be applied to it. Also configuration applied to any other member of a complex type also applies to a member of type Collection.
public class Invoice
{
public int Id { get; set; }
[Aggregation]
public List<Address> Addresses { get; set; }
[Composition]
public List<InvoiceRow> Rows { get; set; }
}
services.AddDbContext<MainDbContext>(dbContextOptions =>
{
...
dbContextOptions.UseMapping(mappingOptions => {
mappingOptions.Default(profileOptions => {
profileOptions.Type<Invoice>()
.Member(i => i.Addresses).Aggregation()
.Member(i => i.Rows).Composition();
});
});
});