You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中无法创建Entity Framework迁移的问题求助

ASP.NET MVC EF迁移报错:Exception has been thrown by the target of an invocation

问题详情

在ASP.NET MVC项目中使用Entity Framework的DbContext和DbSet创建数据库,执行Enable-Migrations(已确认默认启用)和Add-Migration Initial时,始终抛出异常:

Exception has been thrown by the target of an invocation

已按提示启用<loadFromRemoteSources enabled='true'/>,新建项目排查代码、确认所有版本为最新后,问题仍存在。完整错误信息如下:

PM> Enable-Migrations
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileLoadException: Could not load file or assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See <a href="http://go.microsoft.com/fwlink/?LinkId=155569" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=155569</a> for more information.

--- End of inner exception stack trace ---
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at System.Data.Entity.Infrastructure.Design.Executor..ctor(String assemblyFile, IDictionary`2 anonymousArguments)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Data.Entity.Tools.AppDomainExecutor..ctor(String assembly, String dataDirectory, String configurationFile, String rootNamespace, String language)
at System.Data.Entity.Tools.Commands.ProjectCommandBase.CreateExecutor()
at System.Data.Entity.Tools.Commands.MigrationsEnableCommand.Execute()
at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
at System.Data.Entity.Tools.Program.Main(String[] args)
Exception has been thrown by the target of an invocation.

解决方案

  • 解除程序集锁定:如果项目文件来自网络下载或压缩包,Windows会添加安全标记。右键项目输出的Test.dll(或对应项目dll),选择「属性」,在「常规」选项卡底部点击「解除锁定」,重新编译项目。
  • 确认配置文件修改位置:将<loadFromRemoteSources enabled="true"/>添加到正确的配置文件节点中:
    • Web项目:修改web.config的<runtime>节点内;
    • 类库/控制台项目:修改app.config的<runtime>节点内。示例配置:
      <runtime>
        <loadFromRemoteSources enabled="true"/>
      </runtime>
      
  • 迁移项目到本地目录:若项目存放在网络共享路径、OneDrive等同步文件夹中,将项目移动到本地磁盘(如C盘非同步目录),避免.NET将其识别为网络位置。
  • 清理重建项目:
    1. 删除项目的bin和obj文件夹;
    2. 在Visual Studio中执行「生成」→「清理解决方案」;
    3. 重新生成解决方案后,再尝试执行迁移命令。
  • 匹配EF与MVC版本:确保Entity Framework版本与ASP.NET MVC版本兼容,老旧MVC项目可尝试降级到稳定兼容的EF版本(如EF 6.4.4),而非直接使用最新版。

内容的提问来源于stack exchange,提问作者Dr Xizt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 20:21:11