Skip to content

Commit

Permalink
📝 Add annotations and conventions to modeling docs
Browse files Browse the repository at this point in the history
Also changing the modeling docs to be split into topics
The relationship content is still missing but I'll add that in a follow up PR
  • Loading branch information
rowanmiller committed Nov 11, 2015
1 parent 9ee649a commit 8706c3c
Show file tree
Hide file tree
Showing 53 changed files with 1,717 additions and 288 deletions.
57 changes: 57 additions & 0 deletions docs/modeling/alternate-keys.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.. include:: /rc1-notice.txt

Alternate Keys
==============

An alternate key serves as a alternate unique identifier for each entity instance in addition to the primary key. When using a relational database this maps to the concept of a unique index/constraint. In EF, alternate keys provide greater functionality than unique :doc:`indexes` because they can be used as the target of a foreign key.

Alternate keys are typically introduced for you when needed and you do not need to manually configure them. See `Conventions`_ for more details.

.. contents:: In this article:
:depth: 3

Conventions
-----------

By convention, an alternate key is introduced for you when you identify a property, that is not the primary key, as the target of a relationship.

.. literalinclude:: configuring/sample/EFModeling.Conventions/Samples/AlternateKey.cs
:language: c#
:lines: 6-37
:emphasize-lines: 12
:linenos:

Data Annotations
----------------

Alternate keys can not be configured using Data Annotations.

Fluent API
----------

You can use the Fluent API to configure a single property to be an alternate key.

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/AlternateKeySingle.cs
:language: c#
:lines: 5-22
:emphasize-lines: 7-8
:linenos:

You can also use the Fluent API to configure multiple properties to be an alternate key (known as a composite alternate key).

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/AlternateKeyComposite.cs
:language: c#
:lines: 5-23
:emphasize-lines: 7-8
:linenos:

Relational Databases
--------------------

By convention, the index and constraint in the database will be named ``AK_<type name>_<property name>``. For composite alternate keys ``<property name>`` becomes an underscore separated list of property names. You can change this using the Fluent API.

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/Relational/AlternateKeyName.cs
:language: c#
:lines: 5-22
:emphasize-lines: 9
:linenos:
50 changes: 50 additions & 0 deletions docs/modeling/concurrency.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. include:: /rc1-notice.txt

Concurrency Tokens
==================

If a property is configured as a concurrency token then EF will check that no other user has modified that value in the database when saving changes to that record. EF uses an optimistic concurrency pattern, meaning it will assume the value has not changed and try to save the data, but throw if it finds the value has been changed.

For example we may want to configure ``SocialSecurityNumber`` on ``Person`` to be a concurrency token. This means that if one user tries to save some changes to a Person, but another user has changed the ``SocialSecurityNumber`` then an exception will be thrown. This may be desirable so that your application can prompt the user to ensure this record still represents the same actual person before saving their changes.

.. contents:: In this article:
:depth: 3

How concurrency tokens work in EF
---------------------------------

Data stores can enforce concurrency tokens by checking that any record being updated or deleted still has the same value for the concurrency token that was assigned when the context originally loaded the data from the database.

For example, relational database achieve this by including the concurrency token in the ``WHERE`` clause of any ``UPDATE`` or ``DELETE`` commands and checking the number of rows that were affected. If the concurrency token still matches then one row will be updated. If the value in the database has changed, then no rows are updated.

.. code-block:: sql
UPDATE [Person] SET [Name] = @p1
WHERE [PersonId] = @p0 AND [SocialSecurityNumber] = @p2;
Conventions
-----------

By convention, properties are never configured as concurrency tokens.

Data Annotations
----------------

You can use the Data Annotations to configure a property as a concurrency token.

.. literalinclude:: configuring/sample/EFModeling.Configuring.DataAnnotations/Samples/Concurrency.cs
:language: c#
:lines: 11-17
:emphasize-lines: 4
:linenos:

Fluent API
----------

You can use the Fluent API to configure a property as a concurrency token.

.. literalinclude:: configuring/sample/EFModeling.Configuring.FluentAPI/Samples/Concurrency.cs
:language: c#
:lines: 5-22
:emphasize-lines: 7-9
:linenos:
270 changes: 0 additions & 270 deletions docs/modeling/configuring.rst

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>

0 comments on commit 8706c3c

Please sign in to comment.