{"record":{"id":"e6998d188c1ffcef","repo":"TechnitiumSoftware/DnsServer","slug":"cannot-update-record-new-record-must-be-of-same-t","errorCode":null,"errorMessage":"Cannot update record: new record must be of same type.","messagePattern":"Cannot update record: new record must be of same type\\.","errorType":"exception","errorClass":"DnsServerException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs","lineNumber":2305,"sourceCode":"\n            if (authZone.AddRecord(record))\n            {\n                if (authZone is SubDomainZone subDomainZone)\n                    subDomainZone.AutoUpdateState();\n\n                return true;\n            }\n\n            return false;\n        }\n\n        public void UpdateRecord(string zoneName, DnsResourceRecord oldRecord, DnsResourceRecord newRecord)\n        {\n            ValidateIfDomainBelongsToZone(zoneName, oldRecord.Name);\n            ValidateIfDomainBelongsToZone(zoneName, newRecord.Name);\n\n            if (oldRecord.Type != newRecord.Type)\n                throw new DnsServerException(\"Cannot update record: new record must be of same type.\");\n\n            if (oldRecord.Type == DnsResourceRecordType.SOA)\n                throw new DnsServerException(\"Cannot update record: use SetRecords() for updating SOA record.\");\n\n            if (!_root.TryGet(zoneName, oldRecord.Name, out AuthZone authZone))\n                throw new DnsServerException(\"Cannot update record: zone '\" + zoneName + \"' does not exists.\");\n\n            switch (oldRecord.Type)\n            {\n                case DnsResourceRecordType.CNAME:\n                case DnsResourceRecordType.DNAME:\n                case DnsResourceRecordType.APP:\n                    if (oldRecord.Name.Equals(newRecord.Name, StringComparison.OrdinalIgnoreCase))\n                    {\n                        authZone.SetRecords(newRecord.Type, new DnsResourceRecord[] { newRecord });\n\n                        if (authZone is SubDomainZone subDomainZone)\n                            subDomainZone.AutoUpdateState();","sourceCodeStart":2287,"sourceCodeEnd":2323,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs#L2287-L2323","documentation":"Thrown by UpdateRecord when oldRecord.Type does not equal newRecord.Type. DNS record updates must preserve the resource record type (e.g. A stays A, CNAME stays CNAME). Changing the type is not an update; it requires a delete + add or using SetRecords.","triggerScenarios":"Calling UpdateRecord(zoneName, oldRecord, newRecord) where oldRecord.Type != newRecord.Type. For example, passing an A record as oldRecord and an AAAA record as newRecord. The check fires after ValidateIfDomainBelongsToZone but before zone lookup, so domain validation passes first.","commonSituations":"Client UI that lets the user change the record type in an edit form without deleting and re-adding; API integration that constructs oldRecord and newRecord from different sources; migration script that changes record types in-place; copy-paste error in test data.","solutions":["Ensure oldRecord.Type == newRecord.Type before calling UpdateRecord; if the type must change, call DeleteRecord then AddRecord instead.","Add a client-side guard in the UI to prevent type changes in edit mode.","For bulk type changes, iterate with delete+add pairs rather than UpdateRecord."],"exampleFix":"// before (fails: changing A to AAAA)\nvar oldRec = new DnsResourceRecord(\"host.example.com\", DnsResourceRecordType.A, DnsClass.IN, 300, new DnsARecordData(IPAddress.Parse(\"10.0.0.1\")));\nvar newRec = new DnsResourceRecord(\"host.example.com\", DnsResourceRecordType.AAAA, DnsClass.IN, 300, new DnsAAAARecordData(IPAddress.Parse(\"fd00::1\")));\nauthZoneManager.UpdateRecord(\"example.com\", oldRec, newRec);\n\n// after (delete + add for type change)\nauthZoneManager.DeleteRecord(\"example.com\", oldRec);\nauthZoneManager.AddRecord(\"example.com\", newRec);","handlingStrategy":"validation","validationCode":"if (oldRecord.Type != newRecord.Type)\n    throw new ArgumentException(\"Record type cannot be changed via UpdateRecord; use DeleteRecord + AddRecord instead.\");","typeGuard":"static bool IsSameRecordType(DnsResourceRecord oldRecord, DnsResourceRecord newRecord)\n    => oldRecord.Type == newRecord.Type;","tryCatchPattern":"try\n{\n    authZoneManager.UpdateRecord(zoneName, oldRecord, newRecord);\n}\ncatch (DnsServerException ex) when (ex.Message.Contains(\"new record must be of same type\"))\n{\n    // type changed: delete old + add new\n    authZoneManager.DeleteRecord(zoneName, oldRecord);\n    authZoneManager.AddRecord(zoneName, newRecord);\n}","preventionTips":["In UI edit forms, prevent the user from changing the record type field.","In API integrations, assert oldRecord.Type == newRecord.Type before calling UpdateRecord.","For type changes, always use delete + add pattern."],"tags":["dns-records","zone-management","validation","record-update"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}