1

I´m getting the next error message:

System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. 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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142

2 Answers2

0

You would have to delete all the Devices using that type first... Or disable the foreign key first

Milney
  • 6,253
  • 2
  • 19
  • 33
  • I need to keep the devices. – Raúl Eduarte Aug 09 '18 at 11:23
  • If you delete the Type, then what would you like all devices which DID have that type before it was deleted to have in their Type column after? – Milney Aug 09 '18 at 11:33
  • You can just disable the foreign key to allow you to do this - but you would be making the database design worse... Presumably the foreign key is there for a reason – Milney Aug 09 '18 at 11:34
  • I agree with you, there must be a way to delete an object without having this problem – Raúl Eduarte Aug 09 '18 at 12:08
  • As I mentioned - The way is to disable or remove the foreign key constraint... However you probably don't want to do this. First you must change the type on all the devices using that type to a different type, or remove those devices... Read up on what a Foreign Key is for: https://en.wikipedia.org/wiki/Foreign_key – Milney Aug 09 '18 at 12:16
  • I appreciate your help, I will go ahead and check it out – Raúl Eduarte Aug 09 '18 at 12:48
0

You have a foreign key between Device and Type. 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?)

nvoigt
  • 75,013
  • 26
  • 93
  • 142