简体   繁体   English

参照完整性违规

[英]Referential Integrity violation

I´m getting the next error message: 我收到下一条错误消息:

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. System.Data.Entity.Infrastructure.DbUpdateException:'更新条目时发生错误。 See the inner exception for details.' 有关详细信息,请参见内部异常。

Basically I have two classes, Type and Device. 基本上我有两个类,类型和设备。

public class Device
{
    [Key]
    public int DeviceID { get; set; }
    public string Name { get; set; }
    public virtual Type DeviceType { get; set; 
}

and: 和:

public class Type
{
    [Key]
    public string TypeName { get; set; }
}

How can I delete a Type in my programm without having problems with the databases? 如何在程序中删除类型而不导致数据库出现问题?

您必须先删除使用该类型的所有设备...或先禁用外键

You have a foreign key between Device and Type . 您在DeviceType之间有一个外键。 Depending on how you have set it up, a device may or may not have a type, but once a device has a type, your database will not allow you to delete that type , because what would the type of that device be then? 根据您的设置方式,设备可能具有类型也可能不具有类型,但是一旦设备具有类型,则数据库将不允许您删除该类型 ,因为那个设备的类型是什么?

Your options are: 您的选择是:

  • Do not delete types. 不要删除类型。 Just add a boolean field to note whether the type is active or not. 只需添加一个布尔值字段即可说明该类型是否处于活动状态。

  • Delete types only after you updates all devices with what you want (set them to no type? Delete them too?) 仅在用所需的内容更新所有设备后才删除类型(将它们设置为无类型?也删除它们?)

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

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