{"record":{"id":"d70fb1247ad97e3f","repo":"litedb-org/LiteDB","slug":"enginesettings-must-have-filename-or-datastream-as","errorCode":null,"errorMessage":"EngineSettings must have Filename or DataStream as data source","messagePattern":"EngineSettings must have Filename or DataStream as data source","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/EngineSettings.cs","lineNumber":102,"sourceCode":"        {\n            if (this.DataStream != null)\n            {\n                return new StreamFactory(this.DataStream, this.Password);\n            }\n            else if (this.Filename == \":memory:\")\n            {\n                return new StreamFactory(new MemoryStream(), this.Password);\n            }\n            else if (this.Filename == \":temp:\")\n            {\n                return new StreamFactory(new TempStream(), this.Password);\n            }\n            else if (!string.IsNullOrEmpty(this.Filename))\n            {\n                return new FileStreamFactory(this.Filename, this.Password, this.ReadOnly, false, useAesStream);\n            }\n\n            throw new ArgumentException(\"EngineSettings must have Filename or DataStream as data source\");\n        }\n\n        /// <summary>\n        /// Create new IStreamFactory for logfile\n        /// </summary>\n        internal IStreamFactory CreateLogFactory()\n        {\n            if (this.LogStream != null)\n            {\n                return new StreamFactory(this.LogStream, this.Password);\n            }\n            else if (this.Filename == \":memory:\")\n            {\n                return new StreamFactory(new MemoryStream(), this.Password);\n            }\n            else if (this.Filename == \":temp:\")\n            {\n                return new StreamFactory(new TempStream(), this.Password);","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/EngineSettings.cs#L84-L120","documentation":"Thrown by EngineSettings.CreateDataFactory() when none of the data-source conditions are met: DataStream is null, Filename is not ':memory:', not ':temp:', and not a non-empty string path. The factory cannot create an IStreamFactory without a backing data source, so engine initialization aborts. This is an ArgumentException, not a LiteException.","triggerScenarios":"Constructing an EngineSettings (or LiteDatabase with a connection string) where both Filename and DataStream are left null/empty. Happens when building settings programmatically and forgetting to assign a data source.","commonSituations":"Dependency injection registering LiteDatabase with a blank connection string; deserializing settings from JSON where Filename is missing; copy-paste from a sample that used DataStream when the app intended a file path.","solutions":["Set EngineSettings.Filename to a valid file path, ':memory:', or ':temp:'.","Or set EngineSettings.DataStream to a Stream (MemoryStream / custom) for in-memory or custom backing.","Validate settings before passing to the engine constructor.","If using a connection string, ensure the filename= key is present and non-empty."],"exampleFix":"// before\nvar settings = new EngineSettings(); // no Filename, no DataStream\nvar db = new LiteEngine(settings); // throws\n\n// after — file path\nvar db = new LiteDatabase(\"MyData.db\");\n// or in-memory\nvar db = new LiteDatabase(\":memory:\");\n// or via settings\nvar settings = new EngineSettings { Filename = \"MyData.db\" };","handlingStrategy":"validation","validationCode":"public LiteDatabase CreateDatabase(string filename, Stream dataStream = null, string password = null)\n{\n    if (string.IsNullOrWhiteSpace(filename) && dataStream == null)\n        throw new ArgumentException(\"Either filename or DataStream must be provided.\");\n    var cs = $\"Filename={filename};Password={password}\";\n    return new LiteDatabase(cs);\n}","typeGuard":"static bool HasDataSource(EngineSettings s) =>\n    !string.IsNullOrWhiteSpace(s.Filename) || s.DataStream != null;","tryCatchPattern":"try\n{\n    var db = new LiteDatabase(settings);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Filename or DataStream\"))\n{\n    throw new ConfigurationException(\"Database connection is missing a Filename or DataStream.\", ex);\n}","preventionTips":["Always set Filename or DataStream before constructing the engine.","Validate connection strings at app startup with a health check.","Use ':memory:' or ':temp:' for ephemeral databases rather than leaving Filename null.","Add a DI registration test that opens the DB to catch missing config early."],"tags":["engine-settings","configuration","initialization","litedb-engine"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}