Skip to content

Preserve DeleteBehavior when scaffolding a database created by EF Core #21252

Closed

Description

I have a test database where I set the DeleteBehaviour of one line to Restrict. When I reverse engineer it the DeleteBehaviour is set to ClientSetNull. As the foreign key isn't nullable ClientSetNull seems an odd result.

Steps to reproduce

My DbContext looks like this

public class EfCoreContext : DbContext
{
    private readonly Guid _userId;                                    

    public EfCoreContext(DbContextOptions<EfCoreContext> options,     
        IUserIdService userIdService = null)                          
        : base(options)
    {
        _userId = userIdService?.GetUserId()                          
                   ?? new ReplacementUserIdService().GetUserId();     
    }

    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<PriceOffer> PriceOffers { get; set; }
    public DbSet<Order> Orders { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BookAuthor>().HasKey(x => new {x.BookId, x.AuthorId});

        modelBuilder.Entity<LineItem>()
            .HasOne(p => p.ChosenBook) 
            .WithMany()
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<Book>().HasQueryFilter(p => !p.SoftDeleted);                     
                                                        
        modelBuilder.Entity<Order>() .HasQueryFilter(x => x.CustomerId == _userId);            
    } 
}

The part of the reverse engineered that configures the LineItem is

modelBuilder.Entity<LineItem>(entity =>
{
    entity.ToTable("LineItem");

    entity.HasIndex(e => e.BookId);

    entity.HasIndex(e => e.OrderId);

    entity.Property(e => e.BookPrice).HasColumnType("decimal(18, 2)");

    entity.HasOne(d => d.Book)
        .WithMany(p => p.LineItems)
        .HasForeignKey(d => d.BookId)
        .OnDelete(DeleteBehavior.ClientSetNull);

    entity.HasOne(d => d.Order)
        .WithMany(p => p.LineItems)
        .HasForeignKey(d => d.OrderId);
});

I did check the SQL for the LineItem below

CREATE TABLE [LineItem] (
    [LineItemId] int NOT NULL IDENTITY,
    [LineNum] tinyint NOT NULL,
    [NumBooks] smallint NOT NULL,
    [BookPrice] decimal(18,2) NOT NULL,
    [OrderId] int NOT NULL,
    [BookId] int NOT NULL,
    CONSTRAINT [PK_LineItem] PRIMARY KEY ([LineItemId]),
    CONSTRAINT [FK_LineItem_Books_BookId] FOREIGN KEY ([BookId]) REFERENCES [Books] ([BookId]) ON DELETE NO ACTION,
    CONSTRAINT [FK_LineItem_Orders_OrderId] FOREIGN KEY ([OrderId]) REFERENCES [Orders] ([OrderId]) ON DELETE CASCADE
);

Further technical details

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 5-preview5
Operating system: Windows
IDE: Visual Studio 2019 16.6

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

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions