Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/7.0] Scaffolding: Handle FK on keyless entity type #29779

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(foreignKey.DependentToPrincipal.Name));
this.Write(").");
this.Write(this.ToStringHelper.ToStringWithCulture(foreignKey.IsUnique ? "WithOne" : "WithMany"));
this.Write("(p => p.");
this.Write(this.ToStringHelper.ToStringWithCulture(foreignKey.PrincipalToDependent.Name));
this.Write("(");
this.Write(this.ToStringHelper.ToStringWithCulture(foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : ""));
this.Write(")");
this.Write(this.ToStringHelper.ToStringWithCulture(code.Fragment(foreignKeyFluentApiCalls, indent: 4)));
this.Write(";\r\n");
Expand Down Expand Up @@ -803,7 +803,7 @@ public System.IFormatProvider FormatProvider
{
get
{
return this.formatProviderField;
return this.formatProviderField ;
}
set
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public partial class <#= Options.ContextName #> : DbContext
WriteLine("");
}
#>
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(p => p.<#= foreignKey.PrincipalToDependent.Name #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 4) #>;
entity.HasOne(d => d.<#= foreignKey.DependentToPrincipal.Name #>).<#= foreignKey.IsUnique ? "WithOne" : "WithMany" #>(<#= foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : "" #>)<#= code.Fragment(foreignKeyFluentApiCalls, indent: 4) #>;
<#
anyEntityTypeConfiguration = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public System.IFormatProvider FormatProvider
{
get
{
return this.formatProviderField;
return this.formatProviderField ;
}
set
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,101 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Assert.Equal(new[] { "ColorCode" }, colorNavigation.ForeignKey.PrincipalKey.Properties.Select(p => p.Name));
});

[ConditionalFact]
public Task Foreign_key_from_keyless_table()
=> TestAsync(
modelBuilder => modelBuilder
.Entity("Blog", x => x.Property<int>("Id"))
.Entity("Post", x => x.HasOne("Blog", "Blog").WithMany()),
new ModelCodeGenerationOptions(),
code =>
{
AssertFileContents(
@"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace TestNamespace;

public partial class TestDbContext : DbContext
{
public TestDbContext()
{
}

public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}

public virtual DbSet<Blog> Blog { get; set; }

public virtual DbSet<Post> Post { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning " + DesignStrings.SensitiveInformationWarning + @"
=> optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Id).UseIdentityColumn();
});

modelBuilder.Entity<Post>(entity =>
{
entity.HasNoKey();

entity.HasIndex(e => e.BlogId, ""IX_Post_BlogId"");

entity.HasOne(d => d.Blog).WithMany().HasForeignKey(d => d.BlogId);
});

OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
",
code.ContextFile);

AssertFileContents(
@"using System;
using System.Collections.Generic;

namespace TestNamespace;

public partial class Blog
{
public int Id { get; set; }
}
",
code.AdditionalFiles.First(f => f.Path == "Blog.cs"));

AssertFileContents(
@"using System;
using System.Collections.Generic;

namespace TestNamespace;

public partial class Post
{
public int? BlogId { get; set; }

public virtual Blog Blog { get; set; }
}
",
code.AdditionalFiles.First(f => f.Path == "Post.cs"));
},
model =>
{
var post = model.FindEntityType("TestNamespace.Post");
var foreignKey = Assert.Single(post.GetForeignKeys());
Assert.Equal("Blog", foreignKey.DependentToPrincipal.Name);
Assert.Null(foreignKey.PrincipalToDependent);
});

[ConditionalFact]
public Task InverseProperty_when_navigation_property_with_same_type_and_navigation_name()
=> TestAsync(
Expand Down