question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DDD EF Core Remove issue

See original GitHub issue

Let’s say we have a classes:

public class Order 
{
    public int Id { get; set; }
    public List<OrderItem> OrderItems { get; set; }
    // other properties
}
public class OrderItem
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; }
    // other properties
}

Order has primary key on Id property, OrderItem as well. OrderItem has foreign key to Order with OrderId property. Simple as that. Both are autoincremented.

Is there any way to remove OrderItem from Order with RemoveOrderItem defined method in Order class with using Entity Framework Core and not setting primary key on two fields (Id, OrderId) in OrderItem class (or any different key breaking changes workarounds)?

SO

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
macgyver76commented, Jun 27, 2019

I don’t use any of “Cascade Delete” EF Core functionality because I treat it as anti-pattern.

I agree. Hovewer OrderEntityTypeConfiguration class you provide sets delete rule to cascade when using migrations.

By the way. Here is how I set DeleteBehavior to Restrict globally to avoid cascade.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
    {
        relationship.DeleteBehavior = DeleteBehavior.Restrict;
    }
}
0reactions
macgyver76commented, Jul 12, 2019

I came up with workaround. If your existing relationship do not use cascade - solution is to set DeleteBehavior to Cascade and then use Add-Migration command to generate migration files and update snapshot model but before running Update-Database you need to commend out Up method in migration file in order to not set cascade to database. Indeed, we have cascade in EF but not in database.

@kgrzybek - thanks for your commitment and I am looking forward to new DDD stuff in this repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - EF code first: How to delete a row from an entity's ...
Reading around, the only solution is to delete it using the data context. But according to DDD the domain object shouldn't be aware...
Read more >
Cascade Delete - EF Core
Cascading deletes are needed when a dependent/child entity can no longer be associated with its current principal/parent.
Read more >
Implementing the infrastructure persistence layer with ...
Infrastructure in Entity Framework Core from a DDD perspective ... entity class could add or remove items through its property collections.
Read more >
Removing infrastructure information from domain code
When designing a clean DDD solution, many times you need to pollute your domain with infrastructure code in order to get Entity Framework...
Read more >
DDD: Is an aggregate root responsible for deleting its child ...
Yes, it is a common way to do it. In practice, it will often boil down to letting an ORM or delete cascade...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found