We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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()); } } } }
The text was updated successfully, but these errors were encountered:
Note from triage: waiting for @AndriySvyryd
Sorry, something went wrong.
The above configuration promotes the FK to Post, making it redundant. As a workaround it can be configured from the other side:
Post
modelBuilder .Entity<FeaturedPost>() .HasOne<Post>() .WithOne() .HasForeignKey<FeaturedPost>(e => e.Id) .OnDelete(DeleteBehavior.ClientCascade);
Update workaround for TPT cascade paths mitigation
aecd559
See dotnet/efcore#29169 (comment)
Update workaround for TPT cascade paths mitigation (#4072)
7507400
No branches or pull requests
Consider the simple TPT model:
This results in the following tables:
I want to change the cascade behavior for the FK constraint between the two tables, so I do this:
Now the FK constraint disappears entirely!
Full code:
The text was updated successfully, but these errors were encountered: