Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework - Port WithRequired to EF Core

I'm porting an application from MVC5/EF6 to MVC6/EF7, but having an issue with this particular line:

        modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithRequired(e => e.Client).WillCascadeOnDelete(false);

Apparently the WillCascadeOnDelete is transformed to the OnDelete with restrict as parameter, but I can't find any documentation on the "WithRequired" part which has disappeared too in EF7. Has 'WithOne' the same impact or am I completely wrong here :

        modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithOne(e => e.Client).OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
like image 814
Lonefish Avatar asked Mar 28 '26 00:03

Lonefish


1 Answers

You are correct in both cases. Here is a detailed post... http://www.mikee.se/posts/migrating_from_ef6_to_ef_core

Typically these mappings would change from this in EF6.

x.Entity<Company>()
    .HasMany(c => c.StatementOfEarnings)
    .WithRequired(e => e.Company)
    .WillCascadeOnDelete(false);

To this in EF Core

x.Entity<Company>()
    .HasMany(c => c.StatementOfEarnings)
    .WithOne(e => e.Company)
    .OnDelete(DeleteBehavior.Restrict);
like image 102
Teddy Avatar answered Mar 30 '26 10:03

Teddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!