-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd89d13
commit 1faed6a
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
.. include:: /_shared/rc1-notice.txt | ||
|
||
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 <https://github.com/aspnet/EntityFramework/issues/4461>`_ is tracking enabling the ``ChangeTracker`` to write to backing fields for properties with no setter. | ||
|
||
.. contents:: In this article: | ||
:depth: 2 | ||
:local: | ||
|
||
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> | ||
|
||
.. literalinclude:: configuring/sample/EFModeling.Conventions/Samples/BackingField.cs | ||
:language: c# | ||
:lines: 18-29 | ||
:emphasize-lines: 3,7-11 | ||
:linenos: | ||
|
||
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. | ||
|
||
.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/BackingField.cs | ||
:language: c# | ||
:lines: 5-23 | ||
:emphasize-lines: 7-9,15,18 | ||
:linenos: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
docs/modeling/configuring/sample/EFModeling.Configuring.FluentAPI/Samples/BackingField.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Data.Entity; | ||
|
||
namespace EFModeling.Configuring.FluentAPI.Samples.BackingField | ||
{ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
docs/modeling/configuring/sample/EFModeling.Conventions/Samples/BackingField.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Data.Entity; | ||
using System.Collections.Generic; | ||
|
||
namespace EFModeling.Conventions.Samples.BackingField | ||
{ | ||
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 _url; | ||
|
||
public int BlogId { get; set; } | ||
|
||
public string Url | ||
{ | ||
get { return _url; } | ||
set { _url = value; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters