简体   繁体   English

防止实体框架将外键设置为NULL

[英]Prevent Entity Framework from setting foreign key to NULL

Our database has three entities: Person , Company and PhoneCall . 我们的数据库具有三个实体: PersonCompanyPhoneCall

Person describes an individual. Person描述一个人。 Company describes a company. Company描述公司。 PhoneCall describes the details of a phone call to either a Person or Company . PhoneCall描述了打给PersonCompany的电话的详细信息。

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 . 删除PersonCompany时,我发现Entity Framework出现某些不良行为。

If I delete a Person , entity framework is updating any associated PhoneCall entries and setting PersonId to NULL. 如果删除Person ,则实体框架将更新所有关联的PhoneCall条目并将PersonId设置为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 . 我不希望实体框架在所有关联的PhoneCall条目上都将PersonId设置为null,而是对实体框架抛出某种异常,以使我知道无法删除Person因为有相关实体引用了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. 问题似乎是实体框架不尊重PhoneCall.PersonIdPhoneCall.CompanyId外键,因为它们都是(必须)可以为空。

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. ...但是我的偏好是设置某种限制,以防止Entity Framework首先将外键设置为NULL,这样,如果开发人员忘记执行此检查,则最终不会PhoneCall表中的PhoneCall条目PhoneCall链接。

Is this possible? 这可能吗?

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. 如果将属性设置为可为空,则告诉框架您可以根据需要将其设置为null。 Then why would it show any error when it is set to null? 那么为什么将其设置为null时会显示任何错误?

Either you make the property non-nullable or have explicit checks in your code to handle it. 您可以使该属性不可为空,或者在代码中进行显式检查以对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM