Share via


Unable to insert or update an entity because the principal end of the 'TMSModel.FK_AuditInfo_Technology' relationship is deleted.

Question

Friday, July 26, 2013 7:47 AM

I have the following two model classes:-

public partial class Technology

    { public Technology()

        {

            this.AuditInfoes = new HashSet<AuditInfo>();

            this.SwitchPorts = new HashSet<SwitchPort>();

            this.TechnologyIPs = new HashSet<TechnologyIP>();

        }

   

        public int TechnologyID { get; set; }

        public string Tag { get; set; }

        public bool IsDeleted { get; set; }

        public byte[] timestamp { get; set; }

        public Nullable<int> TypeID { get; set; }

        public Nullable<System.DateTime> StartDate { get; set; }

   

        public virtual ICollection<AuditInfo> AuditInfoes { get; set; }

        public virtual Firewall Firewall { get; set; }

        public virtual Rack Rack { get; set; }

        public virtual Router Router { get; set; }

        public virtual Server Server { get; set; }

        public virtual StorageDevice StorageDevice { get; set; }

        public virtual Switch Switch { get; set; }

        public virtual ICollection<SwitchPort> SwitchPorts { get; set; }

        public virtual TechnologyType TechnologyType { get; set; }

        public virtual ICollection<TechnologyIP> TechnologyIPs { get; set; }

        public virtual VirtualMachine VirtualMachine { get; set; }

    }

public partial class AuditInfo

    {

        public int AuditInfoID { get; set; }

        public System.DateTime DateTimeStart { get; set; }

        public int ActionID { get; set; }

        public int AssetTypeID { get; set; }

        public int TechnologyID { get; set; }

        public string UserName { get; set; }

        public System.DateTime DateTimeEnd { get; set; }

   

        public virtual AuditAction AuditAction { get; set; }

        public virtual TechnologyType TechnologyType { get; set; }

        public virtual Technology Technology { get; set; }

    }

But when I tried to delete a Technology record , that does not have any Audit info record I will get the following error:-

 

System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code

  HResult=-2146233087

  Message=Unable to insert or update an entity because the principal end of the 'TMSModel.FK_AuditInfo_Technology' relationship is deleted.

  Source=EntityFramework

My delete repository method is :-

public void DeleteTechnology(int id)
        {
            var technology = FindTechnology(id);
            var auditinfo = IntiateAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "DELETE").ActionID,
                technology.TechnologyType.AssetTypeID,
                    
                 technology.TechnologyID);
            
            tms.Technologies.Remove(technology);
           
            InsertOrUpdateAudit(auditinfo);
        }

Baring in mind that if i directly try to delete the related technology record from the database , it will be removed without raising any exceptions.

All replies (3)

Friday, July 26, 2013 8:07 AM

What happens if you commit your InsertOrUpdateAudit(auditinfo) before you remove the technology object?  And by commit, I mean save to db, then remove the technology object.


Friday, July 26, 2013 8:15 AM

johnjohn123123

try {

var technology = FindTechnology(id);
            var auditinfo = IntiateAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "DELETE").ActionID,
                technology.TechnologyType.AssetTypeID,
                    
                 technology.TechnologyID);
            
            tms.Technologies.Remove(technology);
           
            InsertOrUpdateAudit(auditinfo);
}
catch { 
   System.Entity.DBUpdateException //syntax need correct
}

i am presenting as looking for innerException that could reveal the root caouse. i usually degub as the technique i contended.


Friday, July 26, 2013 8:58 AM

What happens if you commit your InsertOrUpdateAudit(auditinfo) before you remove the technology object?  And by commit, I mean save to db, then remove the technology object.

but doing so will break my buisness login , as i need both the audit commit and the Technology commit to be sent as a single transaction, so either all succeed or none ..