Mapping to entities - an object with a member decorated with a KeyAttribute
- uses a couple of extra rules.
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.
A nullable numeric member named Id will not be mapped to zero if the target type also has a Entity-type member.
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