Skip to content

Commit

Permalink
Scaffolding: Handle FK on keyless entity type
Browse files Browse the repository at this point in the history
Fixes #29516
  • Loading branch information
bricelam committed Dec 5, 2022
1 parent bf20e32 commit 13c8e43
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
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 @@ -1656,6 +1656,101 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Assert.Equal(new[] { "Id1", "Id2" }, blogNavigation.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 { UseDataAnnotations = true },
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

0 comments on commit 13c8e43

Please sign in to comment.