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.Id
will not be mapped, because it is an entity key member -
Category.ParentId
will not be mapped to 0, becauseCategory
has aParent
member -
Category.TopProductId
will be mapped to 0, becauseCategory
has noTopProduct
member