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

Explicit configuration of a TPT relationship causes the database constraint not to be created #29169

Open
ajcvickers opened this issue Sep 20, 2022 · 2 comments

Comments

@ajcvickers
Copy link
Member

Consider the simple TPT model:

[Table("FeaturedPosts")]
public class FeaturedPost : Post
{
}

[Table("Posts")]
public class Post
{
    public int Id { get; set; }
    public string? Title { get; set; }
    public string? Content { get; set; }
}

This results in the following tables:

      CREATE TABLE [Posts] (
          [Id] int NOT NULL IDENTITY,
          [Title] nvarchar(max) NULL,
          [Content] nvarchar(max) NULL,
          CONSTRAINT [PK_Posts] PRIMARY KEY ([Id])
      );

      CREATE TABLE [FeaturedPosts] (
          [Id] int NOT NULL,
          CONSTRAINT [PK_FeaturedPosts] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_FeaturedPosts_Posts_Id] FOREIGN KEY ([Id]) REFERENCES [Posts] ([Id]) ON DELETE CASCADE
      );

I want to change the cascade behavior for the FK constraint between the two tables, so I do this:

modelBuilder
    .Entity<Post>()
    .HasOne<FeaturedPost>()
    .WithOne()
    .HasForeignKey<FeaturedPost>(e => e.Id)
    .OnDelete(DeleteBehavior.ClientCascade);

Now the FK constraint disappears entirely!

      CREATE TABLE [FeaturedPosts] (
          [Id] int NOT NULL IDENTITY,
          CONSTRAINT [PK_FeaturedPosts] PRIMARY KEY ([Id])
      );

      CREATE TABLE [Posts] (
          [Id] int NOT NULL IDENTITY,
          [Title] nvarchar(max) NULL,
          [Content] nvarchar(max) NULL,
          CONSTRAINT [PK_Posts] PRIMARY KEY ([Id])
      );

Full code:

[Table("FeaturedPosts")]
public class FeaturedPost : Post
{
}

[Table("Posts")]
public class Post
{
    public int Id { get; set; }
    public string? Title { get; set; }
    public string? Content { get; set; }
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Your.ConnectionString)
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Post>()
            .HasOne<FeaturedPost>()
            .WithOne()
            .HasForeignKey<FeaturedPost>(e => e.Id)
            .OnDelete(DeleteBehavior.ClientCascade);
        
        modelBuilder.Entity<FeaturedPost>();
    }
}

public class Program
{
    public static void Main()
    {
        using (var context = new SomeDbContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Add(new Post());
            context.Add(new FeaturedPost());
            
            context.SaveChanges();
        }
        
        using (var context = new SomeDbContext())
        {
            foreach (var post in context.Set<Post>().ToList())
            {
                Console.WriteLine(post.GetType());
            }
        }
    }
}
@ajcvickers
Copy link
Member Author

Note from triage: waiting for @AndriySvyryd

@AndriySvyryd
Copy link
Member

The above configuration promotes the FK to Post, making it redundant. As a workaround it can be configured from the other side:

modelBuilder
    .Entity<FeaturedPost>()
    .HasOne<Post>()
    .WithOne()
    .HasForeignKey<FeaturedPost>(e => e.Id)
    .OnDelete(DeleteBehavior.ClientCascade);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants