Mapping to entities - an object with a member decorated with a KeyAttribute - uses a couple of extra rules.
Entity Keys
By default, members decorated with a KeyAttribute are not mapped, unless the mapping is a deep clone. Entity key values are usually generated by a data store, and some ORMs will ignore key value updates, or in some cases, throw an exception.
If you need to map KeyAttribute members, you can configure the behaviour that suits your needs, or configure a custom data source.
Optional Related Entity Keys
A nullable numeric member named
For example:
class CategoryDto
{
public int? Id { get; set; }
public int? ParentId { get; set; }
public int? TopProductId { get; set; }
}
class Category
{
[Key]
public int? Id { get; set; }
public int? ParentId { get; set; }
public Category Parent { get; set; }
public int? TopProductId { get; set; }
}
When mapping from a CategoryDto to a Category:
-
Category.Idwill not be mapped, because it is an entity key member -
Category.ParentIdwill not be mapped to 0, becauseCategoryhas aParentmember -
Category.TopProductIdwill be mapped to 0, becauseCategoryhas noTopProductmember