{"record":{"id":"c091b9e3b784c320","repo":"TechnitiumSoftware/DnsServer","slug":"cannot-add-record-type-record-already-exists-fo","errorCode":null,"errorMessage":"Cannot add record: {type} record already exists for the same name.","messagePattern":"Cannot add record: (.+?) record already exists for the same name\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Zones/AuthZone.cs","lineNumber":817,"sourceCode":"                foreach (DnsResourceRecord nsRecord in nsRecords)\n                    nsRecord.SyncGlueRecords(deletedGlueRecords, addedGlueRecords);\n            }\n        }\n\n        public void LoadRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)\n        {\n            _entries[type] = records;\n        }\n\n        public virtual void SetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records)\n        {\n            switch (type)\n            {\n                case DnsResourceRecordType.CNAME:\n                case DnsResourceRecordType.DNAME:\n                case DnsResourceRecordType.APP:\n                    if ((!_entries.IsEmpty) && !_entries.ContainsKey(type))\n                        throw new InvalidOperationException($\"Cannot add record: {type} record already exists for the same name.\");\n\n                    break;\n\n                case DnsResourceRecordType.NSEC:\n                case DnsResourceRecordType.RRSIG:\n                    break; //ignore\n\n                default:\n                    if (_entries.ContainsKey(DnsResourceRecordType.CNAME))\n                        throw new InvalidOperationException(\"Cannot add record: a CNAME record cannot exists with other record types for the same name.\");\n\n                    break;\n            }\n\n            _entries[type] = records;\n        }\n\n        public virtual bool AddRecord(DnsResourceRecord record)","sourceCodeStart":799,"sourceCodeEnd":835,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Zones/AuthZone.cs#L799-L835","documentation":"AuthZone.SetRecords (public virtual) enforces exclusivity for CNAME, DNAME, and APP record types. If the _entries dictionary is non-empty and does NOT already contain the requested type, it throws InvalidOperationException indicating that the requested type 'already exists' (meaning other incompatible types exist and the requested exclusive type cannot be added). CNAME and DNAME are exclusive per RFC 1034; APP is a Technitium-specific application record that is also treated as exclusive.","triggerScenarios":"Calling SetRecords with type CNAME, DNAME, or APP when the zone node's _entries already contains records of a different type. For example, calling SetRecords(CNAME, ...) when A records already exist, or SetRecords(DNAME, ...) when other types are present.","commonSituations":"Converting an existing A/AAAA record to a CNAME alias via SetRecords without first clearing conflicting records; setting an APP record on a name that already has standard DNS records; zone import logic that doesn't account for exclusive-type rules.","solutions":["Clear all conflicting record types from the zone node before calling SetRecords for CNAME/DNAME/APP.","If updating an existing CNAME/DNAME/APP, the same type already present is allowed (the check only fires when the type is absent but other types exist).","Use a separate name for the exclusive-type record to avoid the conflict."],"exampleFix":"// before\nzone.SetRecords(DnsResourceRecordType.CNAME, cnameList);\n// throws if A/AAAA/MX records exist at this name\n\n// after\nforeach (var t in existingTypes.Where(t => t != DnsResourceRecordType.CNAME))\n    zone.DeleteRecords(t);\nzone.SetRecords(DnsResourceRecordType.CNAME, cnameList);","handlingStrategy":"validation","validationCode":"// For exclusive types (CNAME, DNAME, APP), clear other types first\nvar exclusiveTypes = new[] { DnsResourceRecordType.CNAME, DnsResourceRecordType.DNAME, DnsResourceRecordType.APP };\nif (exclusiveTypes.Contains(type))\n{\n    foreach (var existingType in zone.GetRecordTypes())\n        if (existingType != type)\n            zone.DeleteRecords(existingType);\n}\nzone.SetRecords(type, records);","typeGuard":"static bool IsExclusiveRecordType(DnsResourceRecordType type)\n    => type == DnsResourceRecordType.CNAME\n    || type == DnsResourceRecordType.DNAME\n    || type == DnsResourceRecordType.APP;","tryCatchPattern":null,"preventionTips":["Treat CNAME, DNAME, and APP as exclusive types in all zone-management logic.","Before setting an exclusive type, enumerate and clear all other record types.","Validate zone state in import/export pipelines for exclusive-type compliance."],"tags":["dns","cname","dname","exclusive-record","auth-zone"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}