Ef two relationship

I have the following Entity Framework 5 code classes

public class Airplane { public int Id { get; set; } public int LeftWingId { get; set; } public virtual Wing LeftWing { get; set; } public int RightWingId { get; set; } public virtual Wing RightWing { get; set; } } public class Wing { public int Id { get; set; } } 

The aircraft has one left and one right wing (both are necessary). The wing may belong to 0..1 aircraft (as the left or right wing) or to another β€œflying device”. Removing an aircraft should cascade-remove its wings.

How can this be set up using a code-free API?

Is it possible to have two associations 0..1 --- 1 in EF with cascading deletion on both?

+4
source share
1 answer

Unfortunately, you cannot have two associations pointing from the same source table to the same target table with cascading deletion on both.

However, this is not due to the restriction in EF, but on the SQL server.

Quote from answer another question that you can check:

SQL Server makes a simple calculation of cascading paths and instead of trying to find out if any loops exist, it assumes worse and refuses to create referenced actions (CASCADE): you can and should still create constraints without a referenced action. If you cannot change your design (or it can compromise things), then you should consider using triggers as a last resort.

+1
source

Source: https://habr.com/ru/post/1438869/


All Articles