Prevent Entity Framework from setting foreign key to NULL

NoPyGod

Our database has three entities: Person, Company and PhoneCall.

Person describes an individual. Company describes a company. PhoneCall describes the details of a phone call to either a Person or Company.

public class Person
{
    public int Id { get; set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class Company
{
    public int Id { get; set;}
    public string Name { get; set; }
    public string VatNumber { get; set; }
}

public class PhoneCall
{
    public int Id { get; set;}
    public string Description { get; set;}
    public int Duration { get; set;}

    public int? PersonId { get; set; }
    public Person Person { get; set; }

    public int? CompanyId { get; set; }
    public Company Company { get; set; }

}

I am seeing certain undesirable behavior with Entity Framework when deleting a Person or Company.

If I delete a Person, entity framework is updating any associated PhoneCall entries and setting PersonId to NULL.

dbContext.Entry(person).State = EntityState.Deleted;
dbContext.SaveChanges();

Instead of Entity Framework setting PersonId to null on all of the associated PhoneCall entries, what I would like is for entity framework throw some kind exception to let me know that the Person cannot be deleted because there are related entities referencing said Person.

The problem seems to be that entity framework doesn't respect the PhoneCall.PersonId and PhoneCall.CompanyId foreign keys since they are both (necessarily) nullable.

I can obviously perform my own check before deletion like so:

if (phoneCallDbSet.Where(ph => ph.Person == personToDelete).Any())
    throw new InvalidOperationException("Cannot delete person with associated calls");

...but my preference would be to set up a some kind of restriction that prevents Entity Framework from setting the foreign key to NULL in the first place, so that if a developer forgets to perform this check, we don't end up with entries in the PhoneCall table linking to nothing.

Is this possible?

Jatin Nath Prusty

No!! There is no way to achieve what you want without having explicit checks in your code.

If we set the property as nullable, we tell the framework that you can set it to null if required. Then why would it show any error when it is set to null?

Either you make the property non-nullable or have explicit checks in your code to handle it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prevent Entity Framework from setting foreign key to NULL

From Dev

Setting a Foreign Key Value Back to Null in Entity Framework

From Dev

Setting association to foreign key to inherited object using graphdiff and Entity Framework

From Dev

Setting association to foreign key to inherited object using graphdiff and Entity Framework

From Dev

Foreign key with Entity Framework

From Dev

Entity Framework - Foreign Key not set (0 / null) but navigation property is not null

From Dev

Entity Framework 5 codefirst / Required and optional foreign key relations are null

From Dev

Foreign Key Entity Framework 6 getting null inserted in dependent table

From Dev

Entity Framework 5 codefirst / Required and optional foreign key relations are null

From Dev

Entity Framework 6 Update where foreign key references NULL

From Dev

Entity Framework - Foreign Key Constraint

From Dev

Entity Framework, Unneeded Foreign key

From Dev

Entity Framework Foreign Key DataAnnotations

From Dev

Entity Framework retrieve data from table with foreign key

From Dev

Entity framework: changing mapped foreign key from required to nullable

From Dev

structure of the classes having foreign key relationship generated from entity framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Entity Framework foreign key inserting duplicate key

From Dev

Entity Framework code first can't update database after setting foreign key as nullable

From Dev

Setting a Foreign Key using ebean in Play Framework

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Cascade delete - FOREIGN KEY constraint

From Dev

Entity Framework foreign key not found during migration

From Java

How to add foreign key relationship in Entity Framework?

From Dev

Entity Framework Foreign Key Error with Insert

From Dev

Entity Framework creates undesired foreign key column

From Dev

Two column foreign key in entity framework

From Dev

Entity Framework 6 Adding foreign key

From Dev

Unable to update Foreign Key in Entity Framework 6

Related Related

  1. 1

    Prevent Entity Framework from setting foreign key to NULL

  2. 2

    Setting a Foreign Key Value Back to Null in Entity Framework

  3. 3

    Setting association to foreign key to inherited object using graphdiff and Entity Framework

  4. 4

    Setting association to foreign key to inherited object using graphdiff and Entity Framework

  5. 5

    Foreign key with Entity Framework

  6. 6

    Entity Framework - Foreign Key not set (0 / null) but navigation property is not null

  7. 7

    Entity Framework 5 codefirst / Required and optional foreign key relations are null

  8. 8

    Foreign Key Entity Framework 6 getting null inserted in dependent table

  9. 9

    Entity Framework 5 codefirst / Required and optional foreign key relations are null

  10. 10

    Entity Framework 6 Update where foreign key references NULL

  11. 11

    Entity Framework - Foreign Key Constraint

  12. 12

    Entity Framework, Unneeded Foreign key

  13. 13

    Entity Framework Foreign Key DataAnnotations

  14. 14

    Entity Framework retrieve data from table with foreign key

  15. 15

    Entity framework: changing mapped foreign key from required to nullable

  16. 16

    structure of the classes having foreign key relationship generated from entity framework

  17. 17

    Exchanged Foreign Key on Entity Framework Code First From data base

  18. 18

    Entity Framework foreign key inserting duplicate key

  19. 19

    Entity Framework code first can't update database after setting foreign key as nullable

  20. 20

    Setting a Foreign Key using ebean in Play Framework

  21. 21

    Entity Framework Code First Foreign Key issue

  22. 22

    Entity Framework Cascade delete - FOREIGN KEY constraint

  23. 23

    Entity Framework foreign key not found during migration

  24. 24

    How to add foreign key relationship in Entity Framework?

  25. 25

    Entity Framework Foreign Key Error with Insert

  26. 26

    Entity Framework creates undesired foreign key column

  27. 27

    Two column foreign key in entity framework

  28. 28

    Entity Framework 6 Adding foreign key

  29. 29

    Unable to update Foreign Key in Entity Framework 6

HotTag

Archive