locked
Foreignkey update issue RRS feed

  • Question

  • User527076549 posted

    HI,

    M having some issue with foreignkey while updating data .

    I have two entities Example

    SchoolEntity.

    {

    Id ,Name,PLace

    }

    StudentsEnity

    Id,

    Name

    [Foreignkey]

    SchoolId

    So when i delete student entity and its soft delete i am not deleting from DB.And Save it. works fine.

    When i save  _context.SaveChanges()  ,it update studenttable and sets foreignkey=null   any idea why it behaves so?

    Thanks in advance

    Thursday, August 20, 2020 2:45 PM

All replies

  • User-1330468790 posted

    Hi amithashenoy,

      

    I think what you mean is "Cascade Delete". A closely related concept also covered by EF Core delete behaviors is the automatic deletion of a child entity when it's relationship to a parent has been severed--this is commonly known as "deleting orphans".

    From documents, it is saying that:

    There are several delete behaviors defined in the DeleteBehavior enumerator type, which can be passed to the OnDelete fluent API to control whether the deletion of a principal/parent entity or the severing of the relationship to dependent/child entities should have a side effect on the dependent/child entities. 

    There are three actions EF can take when a principal/parent entity is deleted or the relationship to the child is severed:

    • The child/dependent can be deleted
    • The child's foreign key values can be set to null
    • The child remains unchanged

    In short, for optional relationships (the FK can be null), the default behavior is "ClientSetNull", which will set the FK property to null in memory but do nothing for database.

      

    Hope this can help you.

    Best regards,

    Sean

    Friday, August 21, 2020 2:50 AM