Skip to content

Commit

Permalink
Remainder of merge work for key modeling
Browse files Browse the repository at this point in the history
Leftovers from 02400d9
  • Loading branch information
roji committed Dec 20, 2019
1 parent 2650eb7 commit 7bbc0eb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@
"source_path": "entity-framework/core/modeling/relational/fk-constraints.md",
"redirect_url": "/ef/core/modeling/relationships#foreign-key-constraint-name",
"redirect_document_id": false
},
{
"source_path": "entity-framework/core/modeling/relational/unique-constraints.md",
"redirect_url": "/ef/core/modeling/keys",
"redirect_document_id": false
}
]
}
10 changes: 7 additions & 3 deletions entity-framework/core/modeling/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You can also configure multiple properties to be the key of an entity - this is

## Primary key name

By convention, on relational databases primary keys are created with the name `PK_<type name>`. You can use the Fluent API to configure the name of the primary key constraint as follows:
By convention, on relational databases primary keys are created with the name `PK_<type name>`. You can configure the name of the primary key constraint as follows:

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/KeyName.cs?name=KeyName&highlight=5)]

Expand All @@ -59,10 +59,14 @@ Alternate keys are typically introduced for you when needed and you do not need

[!code-csharp[Main](../../../samples/core/Modeling/Conventions/AlternateKey.cs?name=AlternateKey&highlight=12)]

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

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/AlternateKeySingle.cs?name=AlternateKeySingle&highlight=4)]

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

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/AlternateKeyComposite.cs?name=AlternateKeyComposite&highlight=4)]

Finally, by convention, the index and constraint that are introduced for an alternate key will be named `AK_<type name>_<property name>` (for composite alternate keys `<property name>` becomes an underscore separated list of property names). You can configure the name of the alternate key's index and unique constraint:

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/AlternateKeyName.cs?name=AlternateKeyName&highlight=5)]
27 changes: 0 additions & 27 deletions entity-framework/core/modeling/relational/unique-constraints.md

This file was deleted.

2 changes: 0 additions & 2 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@
href: core/modeling/relational/sequences.md
- name: Default Values
href: core/modeling/relational/default-values.md
- name: Alternate Keys (Unique Constraints)
href: core/modeling/relational/unique-constraints.md
- name: Inheritance (Relational Database)
href: core/modeling/relational/inheritance.md
- name: Managing Database Schemas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace EFModeling.FluentAPI.Relational.AlternateKeyName
{
#region Model
class MyContext : DbContext
{
public DbSet<Car> Cars { get; set; }

#region AlternateKeyName
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasAlternateKey(c => c.LicensePlate)
.HasName("AlternateKey_LicensePlate");
}
#endregion
}

class Car
Expand All @@ -22,5 +23,4 @@ class Car
public string Make { get; set; }
public string Model { get; set; }
}
#endregion
}

0 comments on commit 7bbc0eb

Please sign in to comment.