DDD EF Core Remove issue
See original GitHub issueLet’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)?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree. Hovewer
OrderEntityTypeConfiguration
class you provide sets delete rule to cascade when using migrations.By the way. Here is how I set
DeleteBehavior
toRestrict
globally to avoid cascade.I came up with workaround. If your existing relationship do not use cascade - solution is to set
DeleteBehavior
toCascade
and then useAdd-Migration
command to generate migration files and update snapshot model but before runningUpdate-Database
you need to commend outUp
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.