Skip to content

Commit

Permalink
API Review: revert scaffolding nullability breaking changes (#20824)
Browse files Browse the repository at this point in the history
Reverts parts of d1cd9c7

Part of #20409
  • Loading branch information
roji authored May 5, 2020
1 parent 7793514 commit 6e9c4cb
Show file tree
Hide file tree
Showing 23 changed files with 582 additions and 367 deletions.
14 changes: 9 additions & 5 deletions src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ public override ScaffoldedModel GenerateModel(

// output DbContext .cs file
var dbContextFileName = options.ContextName + FileExtension;
var resultingFiles = new ScaffoldedModel(
new ScaffoldedFile(
options.ContextDir != null
var resultingFiles = new ScaffoldedModel
{
ContextFile = new ScaffoldedFile
{
Path = options.ContextDir != null
? Path.Combine(options.ContextDir, dbContextFileName)
: dbContextFileName,
generatedCode));
Code = generatedCode
}
};

foreach (var entityType in model.GetEntityTypes())
{
Expand All @@ -118,7 +122,7 @@ public override ScaffoldedModel GenerateModel(
// output EntityType poco .cs file
var entityTypeFileName = entityType.DisplayName() + FileExtension;
resultingFiles.AdditionalFiles.Add(
new ScaffoldedFile(entityTypeFileName, generatedCode));
new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode });
}

return resultingFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CandidateNamingService : ICandidateNamingService
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GenerateCandidateIdentifier(DatabaseTable originalTable)
=> GenerateCandidateIdentifier(originalTable.Name);
=> GenerateCandidateIdentifier(originalTable.Name!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -37,7 +37,7 @@ public virtual string GenerateCandidateIdentifier(DatabaseTable originalTable)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GenerateCandidateIdentifier(DatabaseColumn originalColumn)
=> GenerateCandidateIdentifier(originalColumn.Name);
=> GenerateCandidateIdentifier(originalColumn.Name!);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class RelationalScaffoldingModelFactory : IScaffoldingModelFactory
private readonly ICandidateNamingService _candidateNamingService;
private Dictionary<DatabaseTable, CSharpUniqueNamer<DatabaseColumn>> _columnNamers;
private bool _useDatabaseNames;
private readonly DatabaseTable _nullTable = new DatabaseTable();
private CSharpUniqueNamer<DatabaseTable> _tableNamer;
private CSharpUniqueNamer<DatabaseTable> _dbSetNamer;
private readonly HashSet<DatabaseColumn> _unmappedColumns = new HashSet<DatabaseColumn>();
Expand Down Expand Up @@ -137,8 +138,12 @@ protected virtual string GetPropertyName([NotNull] DatabaseColumn column)
{
Check.NotNull(column, nameof(column));

var table = column.Table;
var usedNames = new List<string> { GetEntityTypeName(table) };
var table = column.Table ?? _nullTable;
var usedNames = new List<string>();
if (column.Table != null)
{
usedNames.Add(GetEntityTypeName(table));
}

if (!_columnNamers.ContainsKey(table))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public virtual SavedModelFiles Save(

Directory.CreateDirectory(outputDir);

var contextPath = Path.GetFullPath(Path.Combine(outputDir, scaffoldedModel.ContextFile.Path));
var contextPath = Path.GetFullPath(Path.Combine(outputDir, scaffoldedModel.ContextFile!.Path));
Directory.CreateDirectory(Path.GetDirectoryName(contextPath));
File.WriteAllText(contextPath, scaffoldedModel.ContextFile.Code, Encoding.UTF8);

Expand All @@ -174,7 +174,7 @@ private static void CheckOutputFiles(
bool overwriteFiles)
{
var paths = scaffoldedModel.AdditionalFiles.Select(f => f.Path).ToList();
paths.Insert(0, scaffoldedModel.ContextFile.Path);
paths.Insert(0, scaffoldedModel.ContextFile!.Path);

var existingFiles = new List<string>();
var readOnlyFiles = new List<string>();
Expand All @@ -184,11 +184,11 @@ private static void CheckOutputFiles(

if (File.Exists(fullPath))
{
existingFiles.Add(path);
existingFiles.Add(path!);

if (File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly))
{
readOnlyFiles.Add(path);
readOnlyFiles.Add(path!);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public static class DatabaseColumnExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string DisplayName([NotNull] this DatabaseColumn column)
=> column.Table.DisplayName() + "." + column.Name;
{
var tablePrefix = column.Table?.DisplayName();
return (!string.IsNullOrEmpty(tablePrefix) ? tablePrefix + "." : "") + column.Name;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -35,7 +38,7 @@ public static string DisplayName([NotNull] this DatabaseColumn column)
/// </summary>
public static bool IsKeyOrIndex([NotNull] this DatabaseColumn column)
{
var table = column.Table;
var table = column.Table!;

if (table.PrimaryKey?.Columns.Contains(column) == true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static class DatabaseForeignKeyExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string DisplayName([NotNull] this DatabaseForeignKey foreignKey)
=> foreignKey.Table.DisplayName() + "(" + string.Join(",", foreignKey.Columns.Select(f => f.Name)) + ")";
=> foreignKey.Table?.DisplayName() + "(" + string.Join(",", foreignKey.Columns.Select(f => f.Name)) + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static class DatabaseTableExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string DisplayName([NotNull] this DatabaseTable table)
=> !string.IsNullOrEmpty(table.Schema) ? table.Schema + "." + table.Name : table.Name;
=> !string.IsNullOrEmpty(table.Schema) ? table.Schema + "." + table.Name : (table.Name ?? "<UNKNOWN>");
}
}
15 changes: 2 additions & 13 deletions src/EFCore.Design/Scaffolding/ScaffoldedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,16 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding
/// </summary>
public class ScaffoldedFile
{
/// <summary>
/// Creates a new instance of <see cref="ScaffoldedFile" />.
/// </summary>
/// <param name="path"> The path of the scaffolded file. </param>
/// <param name="code"> The scaffolded code. </param>
public ScaffoldedFile([NotNull] string path, [NotNull] string code)
{
Path = path;
Code = code;
}

/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value> The path. </value>
public virtual string Path { get; [param: NotNull] set; }
public virtual string? Path { get; [param: NotNull] set; }

/// <summary>
/// Gets or sets the scaffolded code.
/// </summary>
/// <value> The scaffolded code. </value>
public virtual string Code { get; [param: NotNull] set; }
public virtual string? Code { get; [param: NotNull] set; }
}
}
11 changes: 1 addition & 10 deletions src/EFCore.Design/Scaffolding/ScaffoldedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding
/// </summary>
public class ScaffoldedModel
{
/// <summary>
/// Creates a new instance of <see cref="ScaffoldedModel" />.
/// </summary>
/// <param name="contextFile"> The scaffolded context file. </param>
public ScaffoldedModel([NotNull] ScaffoldedFile contextFile)
{
ContextFile = contextFile;
}

/// <summary>
/// Gets or sets the generated file containing the <see cref="DbContext" />.
/// </summary>
/// <value> The generated file containing the <see cref="DbContext" />. </value>
public virtual ScaffoldedFile ContextFile { get; [param: NotNull] set; }
public virtual ScaffoldedFile? ContextFile { get; [param: NotNull] set; }

/// <summary>
/// Gets any additional generated files for the model.
Expand Down
23 changes: 5 additions & 18 deletions src/EFCore.Relational/Scaffolding/Metadata/DatabaseColumn.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// 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;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

#nullable enable

Expand All @@ -15,28 +15,15 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Metadata
/// </summary>
public class DatabaseColumn : Annotatable
{
/// <summary>
/// Creates a new instance of the <see cref="DatabaseColumn" /> class.
/// </summary>
/// <param name="table"> The table containing this column. </param>
/// <param name="name"> The name of the column. </param>
/// <param name="storeType"> The store type of the column. </param>
public DatabaseColumn([NotNull] DatabaseTable table, [NotNull] string name, [NotNull] string storeType)
{
Table = table;
Name = name;
StoreType = storeType;
}

/// <summary>
/// The table that contains this column.
/// </summary>
public virtual DatabaseTable Table { get; [param: NotNull] set; }
public virtual DatabaseTable? Table { get; [param: NotNull] set; }

/// <summary>
/// The column name.
/// </summary>
public virtual string Name { get; [param: NotNull] set; }
public virtual string? Name { get; [param: NotNull] set; }

/// <summary>
/// Indicates whether or not this column can contain <c>NULL</c> values.
Expand All @@ -46,7 +33,7 @@ public DatabaseColumn([NotNull] DatabaseTable table, [NotNull] string name, [Not
/// <summary>
/// The database/store type of the column.
/// </summary>
public virtual string StoreType { get; [param: NotNull] set; }
public virtual string? StoreType { get; [param: CanBeNull] set; }

/// <summary>
/// The default constraint for the column, or <c>null</c> if none.
Expand Down Expand Up @@ -82,6 +69,6 @@ public DatabaseColumn([NotNull] DatabaseTable table, [NotNull] string name, [Not
public virtual ValueGenerated? ValueGenerated { get; set; }

/// <inheritdoc />
public override string ToString() => Name;
public override string ToString() => Name ?? "<UNKNOWN>";
}
}
27 changes: 5 additions & 22 deletions src/EFCore.Relational/Scaffolding/Metadata/DatabaseForeignKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -15,44 +16,26 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Metadata
/// </summary>
public class DatabaseForeignKey : Annotatable
{
/// <summary>
/// Creates a new instance of the <see cref="DatabaseForeignKey" /> class.
/// </summary>
/// <param name="table"> The table containing this foreign key constraint. </param>
/// <param name="name"> The name of the foreign key constraint. </param>
/// <param name="principalTable"> The table to which the columns are constrained. </param>
public DatabaseForeignKey(
[NotNull] DatabaseTable table,
[CanBeNull] string? name,
[NotNull] DatabaseTable principalTable)
{
Table = table;
Name = name;
PrincipalTable = principalTable;
Columns = new List<DatabaseColumn>();
PrincipalColumns = new List<DatabaseColumn>();
}

/// <summary>
/// The table that contains the foreign key constraint.
/// </summary>
public virtual DatabaseTable Table { get; [param: NotNull] set; }
public virtual DatabaseTable? Table { get; [param: NotNull] set; }

/// <summary>
/// The table to which the columns are constrained.
/// </summary>
public virtual DatabaseTable PrincipalTable { get; [param: NotNull] set; }
public virtual DatabaseTable? PrincipalTable { get; [param: NotNull] set; }

/// <summary>
/// The ordered list of columns that are constrained.
/// </summary>
public virtual IList<DatabaseColumn> Columns { get; }
public virtual IList<DatabaseColumn> Columns { get; } = new List<DatabaseColumn>();

/// <summary>
/// The ordered list of columns in the <see cref="PrincipalTable" /> to which the <see cref="Columns" />
/// of the foreign key are constrained.
/// </summary>
public virtual IList<DatabaseColumn> PrincipalColumns { get; }
public virtual IList<DatabaseColumn> PrincipalColumns { get; } = new List<DatabaseColumn>();

/// <summary>
/// The foreign key constraint name.
Expand Down
16 changes: 2 additions & 14 deletions src/EFCore.Relational/Scaffolding/Metadata/DatabaseIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Metadata
/// </summary>
public class DatabaseIndex : Annotatable
{
/// <summary>
/// Creates a new instance of the <see cref="DatabaseIndex" /> class.
/// </summary>
/// <param name="table"> The table containing this index. </param>
/// <param name="name"> The name of the index. </param>
public DatabaseIndex([NotNull] DatabaseTable table, [CanBeNull] string? name)
{
Table = table;
Name = name;
Columns = new List<DatabaseColumn>();
}

/// <summary>
/// The table that contains the index.
/// </summary>
public virtual DatabaseTable Table { get; [param: NotNull] set; }
public virtual DatabaseTable? Table { get; [param: CanBeNull] set; }

/// <summary>
/// The index name.
Expand All @@ -39,7 +27,7 @@ public DatabaseIndex([NotNull] DatabaseTable table, [CanBeNull] string? name)
/// <summary>
/// The ordered list of columns that make up the index.
/// </summary>
public virtual IList<DatabaseColumn> Columns { get; }
public virtual IList<DatabaseColumn> Columns { get; } = new List<DatabaseColumn>();

/// <summary>
/// Indicates whether or not the index constrains uniqueness.
Expand Down
16 changes: 2 additions & 14 deletions src/EFCore.Relational/Scaffolding/Metadata/DatabasePrimaryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Metadata
/// </summary>
public class DatabasePrimaryKey : Annotatable
{
/// <summary>
/// Creates a new instance of the <see cref="DatabasePrimaryKey" /> class.
/// </summary>
/// <param name="table"> The table on which this primary key is defined. </param>
/// <param name="name"> The name of the primary key. </param>
public DatabasePrimaryKey([NotNull] DatabaseTable table, [CanBeNull] string? name)
{
Table = table;
Name = name;
Columns = new List<DatabaseColumn>();
}

/// <summary>
/// The table on which the primary key is defined.
/// </summary>
public virtual DatabaseTable Table { get; [param: NotNull] set; }
public virtual DatabaseTable? Table { get; [param: CanBeNull] set; }

/// <summary>
/// The name of the primary key.
Expand All @@ -39,7 +27,7 @@ public DatabasePrimaryKey([NotNull] DatabaseTable table, [CanBeNull] string? nam
/// <summary>
/// The ordered list of columns that make up the primary key.
/// </summary>
public virtual IList<DatabaseColumn> Columns { get; }
public virtual IList<DatabaseColumn> Columns { get; } = new List<DatabaseColumn>();

/// <inheritdoc />
public override string ToString() => Name ?? "<UNKNOWN>";
Expand Down
Loading

0 comments on commit 6e9c4cb

Please sign in to comment.