Skip to content

Commit

Permalink
Stop scaffolding DbSet initializers
Browse files Browse the repository at this point in the history
Fixes #26877
  • Loading branch information
bricelam committed Aug 11, 2022
1 parent 13c2a5f commit 0e5c831
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(entityType.Name));
this.Write("> ");
this.Write(this.ToStringHelper.ToStringWithCulture(entityType.GetDbSetName()));
this.Write(" { get; set; }");
this.Write(this.ToStringHelper.ToStringWithCulture(Options.UseNullableReferenceTypes ? " = null!;" : ""));
this.Write("\r\n\r\n");
this.Write(" { get; set; }\r\n\r\n");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public partial class <#= Options.ContextName #> : DbContext
foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
{
#>
public virtual DbSet<<#= entityType.Name #>> <#= entityType.GetDbSetName() #> { get; set; }<#= Options.UseNullableReferenceTypes ? " = null!;" : "" #>
public virtual DbSet<<#= entityType.Name #>> <#= entityType.GetDbSetName() #> { get; set; }

<#
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public void DbSets_with_nrt()
},
code =>
{
Assert.Contains("DbSet<Entity> Entity { get; set; } = null!;", code.ContextFile.Code);
Assert.Contains("DbSet<Entity> Entity { get; set; }", code.ContextFile.Code);
Assert.DoesNotContain("DbSet<Entity> Entity { get; set; } = null!;", code.ContextFile.Code);
},
null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.EntityFrameworkCore.TestUtilities;

Expand Down Expand Up @@ -83,19 +85,22 @@ public Assembly BuildInMemory()
references.AddRange(reference.References);
}

var compilation = CSharpCompilation.Create(
projectName,
Sources.Select(
s => SyntaxFactory.ParseSyntaxTree(
text: s.Value,
path: s.Key,
options: new CSharpParseOptions(
LanguageVersion.Latest,
EmitDocumentationDiagnostics ? DocumentationMode.Diagnose : DocumentationMode.Parse))),
references,
CreateOptions());

var diagnostics = compilation.GetDiagnostics();
var compilation = CSharpCompilation
.Create(
projectName,
Sources.Select(
s => SyntaxFactory.ParseSyntaxTree(
text: s.Value,
path: s.Key,
options: new CSharpParseOptions(
LanguageVersion.Latest,
EmitDocumentationDiagnostics ? DocumentationMode.Diagnose : DocumentationMode.Parse))),
references,
CreateOptions())
.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(new UninitializedDbSetDiagnosticSuppressor()));

// TODO: await
var diagnostics = compilation.GetAllDiagnosticsAsync().GetAwaiter().GetResult();
if (!diagnostics.IsEmpty)
{
throw new InvalidOperationException(
Expand All @@ -114,7 +119,7 @@ public Assembly BuildInMemory()
Assembly assembly;
using (var stream = new MemoryStream())
{
var result = compilation.Emit(stream);
var result = compilation.Compilation.Emit(stream);
if (!result.Success)
{
throw new InvalidOperationException(
Expand Down

0 comments on commit 0e5c831

Please sign in to comment.