-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attribute for configuring composite primary keys
Fixes #11003 This PR introduces a new `[PrimaryKey]` which follows the same pattern as `[Index]` in that it is applied to the entity type class and takes an ordered list of property names. It takes precedence over any `[Key]` attributes on properties, since these may still be needed for OData or other technologies. `PrimaryKey` and `Keyless` cannot be used on the same type.
- Loading branch information
1 parent
ac143e6
commit af7cc98
Showing
15 changed files
with
690 additions
and
90 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,36 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Specifies a primary key for the entity type mapped to this CLR type. This attribute can be used for both keys made up of a | ||
/// single property, and for composite keys made up of multiple properties. `System.ComponentModel.DataAnnotations.KeyAttribute` | ||
/// can be used instead for single-property keys, in which case the behavior is identical. If both attributes are used, then | ||
/// this attribute takes precedence. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class PrimaryKeyAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="IndexAttribute" /> class. | ||
/// </summary> | ||
/// <param name="propertyNames">The properties which constitute the index, in order (there must be at least one).</param> | ||
public PrimaryKeyAttribute(params string[] propertyNames) | ||
{ | ||
Check.NotEmpty(propertyNames, nameof(propertyNames)); | ||
Check.HasNoEmptyElements(propertyNames, nameof(propertyNames)); | ||
|
||
PropertyNames = propertyNames.ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// The properties which constitute the index, in order. | ||
/// </summary> | ||
public IReadOnlyList<string> PropertyNames { get; } | ||
} |
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
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
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
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
Oops, something went wrong.