Caution

This documentation is for EF7 onwards. For EF6.x and earlier release see http://msdn.com/data/ef.

Note

This article uses EF 7.0.0-rc1 which is the latest pre-release available on NuGet.org. You can find nightly builds of the EF7 code base hosted on https://www.myget.org/F/aspnetvnext/ but we do not maintain up-to-date documentation for nightly builds.

Backing Fields

When a backing field is configured, EF will write directly to that field when materializing entity instances from the database (rather than using the property setter). This is useful when there is no property setter, or the setter contains logic that should not be executed when setting initial property values for existing entities being loaded from the database.

Caution

The ChangeTracker has not yet been enabled to use backing fields when it needs to set the value of a property. This is only an issue for foreign key properties and generated properties - as the change tracker needs to propagate values into these properties. For these properties, a property setter must still be exposed.

Issue #4461 is tracking enabling the ChangeTracker to write to backing fields for properties with no setter.

In this article:

Conventions

By convention, the following fields will be discovered as backing fields for a given property (listed in precedence order):
  • <propertyName> differing only by case
  • _<propertyName>
  • m_<propertyName>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    public class Blog
    {
        private string _url;

        public int BlogId { get; set; }

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }
    }

Data Annotations

Backing fields cannot be configured with data annotations.

Fluent API

There is no top level API for configuring backing fields, but you can use the Fluent API to set annotations that are used to store backing field information.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .Property(b => b.Url)
                .HasAnnotation("BackingField", "_blogUrl");
        }
    }

    public class Blog
    {
        private string _blogUrl;

        public int BlogId { get; set; }
        public string Url => _blogUrl;
    }