-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
AddPrimaryKeyOperation.cs
59 lines (50 loc) · 2.18 KB
/
AddPrimaryKeyOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;
namespace Microsoft.EntityFrameworkCore.Migrations.Operations
{
/// <summary>
/// A <see cref="MigrationOperation" /> to add a new foreign key.
/// </summary>
[DebuggerDisplay("ALTER TABLE {Table} ADD CONSTRAINT {Name} PRIMARY KEY")]
public class AddPrimaryKeyOperation : MigrationOperation, ITableMigrationOperation
{
/// <summary>
/// The schema that contains the table, or <see langword="null" /> if the default schema should be used.
/// </summary>
public virtual string? Schema { get; set; }
/// <summary>
/// The table to which the key should be added.
/// </summary>
public virtual string Table { get; set; } = null!;
/// <summary>
/// The name of the foreign key constraint.
/// </summary>
public virtual string Name { get; set; } = null!;
/// <summary>
/// The ordered-list of column names for the columns that make up the primary key.
/// </summary>
public virtual string[] Columns { get; set; } = null!;
/// <summary>
/// Creates a new <see cref="AddPrimaryKeyOperation" /> from the specified primary key.
/// </summary>
/// <param name="primaryKey"> The primary key. </param>
/// <returns> The operation. </returns>
public static AddPrimaryKeyOperation CreateFrom(IPrimaryKeyConstraint primaryKey)
{
Check.NotNull(primaryKey, nameof(primaryKey));
var operation = new AddPrimaryKeyOperation
{
Schema = primaryKey.Table.Schema,
Table = primaryKey.Table.Name,
Name = primaryKey.Name,
Columns = primaryKey.Columns.Select(c => c.Name).ToArray()
};
operation.AddAnnotations(primaryKey.GetAnnotations());
return operation;
}
}
}