{"record":{"id":"f50d688835ab1e09","repo":"TechnitiumSoftware/DnsServer","slug":"cannot-add-record-a-cname-record-cannot-exists-wi","errorCode":null,"errorMessage":"Cannot add record: a CNAME record cannot exists with other record types for the same name.","messagePattern":"Cannot add record: a CNAME record cannot exists with other record types for the same name\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Zones/AuthZone.cs","lineNumber":135,"sourceCode":"                    rrsigRecord.GetAuthGenericRecordInfo().LastUsedOn = utcNow;\n                    newRecords.Add(rrsigRecord);\n                }\n            }\n\n            return newRecords;\n        }\n\n        #endregion\n\n        #region versioning\n\n        internal bool TrySetRecords(DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> records, out IReadOnlyList<DnsResourceRecord> deletedRecords)\n        {\n            switch (type)\n            {\n                case DnsResourceRecordType.CNAME:\n                    if ((!_entries.IsEmpty) && !_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                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            if (_entries.TryGetValue(type, out IReadOnlyList<DnsResourceRecord> existingRecords))\n            {\n                deletedRecords = existingRecords;\n                return _entries.TryUpdate(type, records, existingRecords);","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Zones/AuthZone.cs#L117-L153","documentation":"Inside AuthZone.TrySetRecords (internal), the CNAME case enforces RFC 1034 section 3.6.2: a CNAME resource record cannot coexist with any other record type at the same DNS name. If the zone's _entries dictionary is non-empty and does NOT already contain a CNAME entry (meaning other record types are present), setting a CNAME throws InvalidOperationException. This is a fundamental DNS protocol constraint, not an arbitrary library restriction.","triggerScenarios":"Calling the internal TrySetRecords method with type=DnsResourceRecordType.CNAME when the zone node (_entries) already contains records of other types (A, AAAA, MX, TXT, etc.). Typically reached indirectly through SetRecords or zone-transfer logic that routes through TrySetRecords.","commonSituations":"Migrating an A record to a CNAME alias without first deleting the existing A record; importing zone data from a misconfigured source; automated provisioning scripts that set CNAME without checking existing records; copying records between zones where the target already has records.","solutions":["Delete all existing records for the name before setting a CNAME (call DeleteRecords for each existing type first).","Use SetRecords with type=CNAME which routes through the same guard — clear _entries of conflicting types first.","Switch to an A/AAAA record pointing to the target IP instead of using a CNAME if coexistence is required.","Validate that _entries is empty or contains only CNAME before attempting the CNAME assignment."],"exampleFix":"// before\nzone.TrySetRecords(DnsResourceRecordType.CNAME, cnameRecords, out _);\n// throws if A records exist\n\n// after\nforeach (var type in zone.GetRecordTypes())\n    zone.DeleteRecords(type);\nzone.TrySetRecords(DnsResourceRecordType.CNAME, cnameRecords, out _);","handlingStrategy":"validation","validationCode":"// Before setting CNAME, ensure no other record types exist\nbool hasOtherTypes = zone.GetRecordTypes().Any(t => t != DnsResourceRecordType.CNAME\n    && t != DnsResourceRecordType.NSEC && t != DnsResourceRecordType.RRSIG);\nif (hasOtherTypes)\n    throw new InvalidOperationException(\"Cannot set CNAME: other record types exist. Delete them first.\");\nzone.SetRecords(DnsResourceRecordType.CNAME, cnameRecords);","typeGuard":null,"tryCatchPattern":"try { zone.SetRecords(DnsResourceRecordType.CNAME, cnameRecords); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CNAME\"))\n{ /* clear conflicting records and retry, or report to user */ }","preventionTips":["Always check for existing non-CNAME records before setting a CNAME.","Treat CNAME as a mutually exclusive record type in provisioning logic.","When importing zone data, validate RFC 1034 CNAME exclusivity before applying."],"tags":["dns","cname","rfc-1034","record-conflict","auth-zone"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}