{"record":{"id":"1ed48596f5baee31","repo":"dotnet/efcore","slug":"could-not-load-assembly-assembly-ensure-it-is","errorCode":null,"errorMessage":"Could not load assembly '{assembly}'. Ensure it is referenced by the startup project '{startupProject}'.","messagePattern":"Could not load assembly '(.+?)'\\. Ensure it is referenced by the startup project '(.+?)'\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"critical","filePath":"src/EFCore.Design/Design/OperationExecutor.cs","lineNumber":85,"sourceCode":"            && new SemanticVersionComparer().Compare(toolsVersion, runtimeVersion) < 0)\n        {\n            _reporter.WriteWarning(DesignStrings.VersionMismatch(toolsVersion, runtimeVersion));\n        }\n    }\n\n    private Assembly Assembly\n    {\n        get\n        {\n            Assembly Create()\n            {\n                try\n                {\n                    return Assembly.Load(new AssemblyName(_targetAssemblyName));\n                }\n                catch (Exception ex)\n                {\n                    throw new OperationException(\n                        DesignStrings.UnreferencedAssembly(_targetAssemblyName, _startupTargetAssemblyName),\n                        ex);\n                }\n            }\n\n            return field ??= Create();\n        }\n    }\n\n    private Assembly StartupAssembly\n        => field\n            ??= Assembly.Load(new AssemblyName(_startupTargetAssemblyName));\n\n    /// <summary>\n    ///     Exposes the underlying operations for testing.\n    /// </summary>\n    [EntityFrameworkInternal]\n    public virtual MigrationsOperations MigrationsOperations","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/OperationExecutor.cs#L67-L103","documentation":"OperationExecutor.Assembly tried Assembly.Load on the target assembly name (the --project / startup-project assembly) and the load threw. The assembly isn't reachable from the application's load context. The inner exception holds the real loader error (FileNotFoundException, BadImageFormatException, etc.).","triggerScenarios":"dotnet ef is invoked with a --project or --startup-project whose output assembly is missing or unloadable. Assembly.Load(new AssemblyName(_targetAssemblyName)) throws at line 81, wrapped at line 85.","commonSituations":"Project not built (no DLL in bin/); wrong assembly name passed; target framework mismatch producing a BadImageFormatException; assembly lives in a path not on the probing path; the startup project doesn't reference the target project.","solutions":["Build the project first: dotnet build, then re-run dotnet ef.","Add a project reference from the startup project to the target project.","Verify the --project / --startup-project paths and that the resulting assembly names are correct.","Match target frameworks across projects (don't load a net8.0 assembly into a net48 process or vice versa where unsupported)."],"exampleFix":"// before\ndotnet ef migrations add Init   // project never built\n// after\ndotnet build\ndotnet ef migrations add Init","handlingStrategy":"try-catch","validationCode":"try { Assembly.Load(new AssemblyName(targetAssemblyName)); }\ncatch (Exception ex) { throw new InvalidOperationException($\"Build the project or add a reference; cannot load '{targetAssemblyName}'.\", ex); }","typeGuard":"static bool IsLoadable(string asmName) { try { _ = Assembly.Load(new AssemblyName(asmName)); return true; } catch { return false; } }","tryCatchPattern":"try { /* dotnet ef invocation */ }\ncatch (OperationException ex) when (ex.Message.Contains(\"Could not load assembly\"))\n{ /* run dotnet build, verify --project path, then retry */ }","preventionTips":["Always 'dotnet build' before 'dotnet ef'.","Reference target projects from the startup project.","Keep target frameworks aligned across projects."],"tags":["efcore","assembly-loading","cli","design-time","build"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}