共用方式為


PropertyAccessMode Enum

Definition

Pass a value from this enum to UsePropertyAccessMode(PropertyAccessMode), UsePropertyAccessMode(PropertyAccessMode), or UsePropertyAccessMode(PropertyAccessMode) to change whether the property or backing field will be used when reading and writing to a property or field.

public enum PropertyAccessMode
type PropertyAccessMode = 
Public Enum PropertyAccessMode
Inheritance
PropertyAccessMode

Fields

Name Value Description
Field 0

Enforces that all accesses to the property must go through the field.

FieldDuringConstruction 1

Enforces that all accesses to the property must go through the field when new instances are being constructed. New instances are typically constructed when entities are queried from the database. An exception will be thrown if this mode is set and it is not possible to write to the field.

Property 2

Enforces that all accesses to the property must go through the property getters and setters, even when new objects are being constructed.

PreferField 3

All accesses to the property goes directly to the field, unless the field is not known, in which case access goes through the property.

PreferFieldDuringConstruction 4

All accesses to the property when constructing new entity instances goes directly to the field, unless the field is not known, in which case access goes through the property. All other uses of the property will go through the property getters and setters, unless this is not possible because, for example, the property is read-only, in which case these accesses will also use the field.

PreferProperty 5

All accesses to the property go through the property, unless there is no property or it is missing a setter/getter, in which case access goes directly to the field.

Remarks

The default behavior is PreferField. Prior to EF Core 3.0, the default behavior was PreferFieldDuringConstruction.

See Property versus field access in EF Core for more information and examples.

Applies to