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

ASP.NET Core 3.1添加迁移时遇COM对象转换异常求助

ASP.NET Core 3.1 EF Core迁移报错:System.InvalidCastException(COM对象转换失败)

错误信息

System.InvalidCastException: 无法将类型为'System.__ComObject'的COM对象转换为接口类型'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'。此操作失败的原因是对COM组件调用QueryInterface以获取IID为'{FFB2E715-7312-4B93-83D7-D37BCC561C90}'的接口时失败,错误原因:不支持此接口(HRESULT异常:0x80004002 (E_NOINTERFACE))。
à System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project)
à System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebSiteProject(Project project)
à System.Data.Entity.Migrations.MigrationsCommands..ctor(Object project, Object startUpProject, String configurationTypeName, String connectionStringName, String connectionString, String connectionProviderName, PSCmdlet cmdlet)
无法将类型为'System.__ComObject'的COM对象转换为接口类型'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'。此操作失败的原因是对COM组件调用QueryInterface以获取IID为'{FFB2E715-7312-4B93-83D7-D37BCC561C90}'的接口时失败,错误原因:不支持此接口(HRESULT异常:0x80004002 (E_NOINTERFACE))。

项目配置文件

appsettings.json

{ 
  "Logging": { 
    "LogLevel": { 
      "Default": "Information", 
      "Microsoft": "Warning", 
      "Microsoft.Hosting.Lifetime": "Information" 
    } 
  }, 
  "AllowedHosts": "*", 
  "ConnectionStrings": { 
    "default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=firstDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" 
  } 
}

Program.cs

public class Program 
{ 
    public static void Main(string[] args) 
    { 
        CreateHostBuilder(args).Build().Run(); 
    } 

    public static IHostBuilder CreateHostBuilder(string[] args) => 
        Host.CreateDefaultBuilder(args) 
            .ConfigureWebHostDefaults(webBuilder => 
            { 
                webBuilder.UseStartup<Startup>(); 
            }); 
}

Startup.cs

public class Startup 
{ 
    public Startup(IConfiguration configuration) 
    { 
        Configuration = configuration; 
    } 

    public IConfiguration Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
       // services.AddControllersWithViews(); 
        string connectionString = Configuration.GetConnectionString("default"); 
        services.AddDbContext<AppDBContext>(c => c.UseSqlServer(connectionString)); 
        services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDBContext>(); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
    { 
        if (env.IsDevelopment()) 
        { 
            app.UseDeveloperExceptionPage(); 
        } 
        else 
        { 
            app.UseExceptionHandler("/Home/Error"); 
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 
            app.UseHsts(); 
        } 
        app.UseHttpsRedirection(); 
        app.UseStaticFiles(); 

        app.UseRouting(); 

        app.UseAuthentication(); 
        app.UseAuthorization(); 

        app.UseEndpoints(endpoints => 
        { 
            endpoints.MapControllerRoute( 
                name: "default", 
                pattern: "{controller=Home}/{action=Index}/{id?}"); 
        }); 
    } 
}

解决方案

1. 明确指定项目参数执行迁移

在Package Manager Console中执行命令时,手动指定启动项目和目标项目,避免Visual Studio COM组件识别错误:

# 分项目场景
Add-Migration InitialCreate -StartupProject 你的Web项目名称 -Project 你的数据项目名称

# 单项目场景
Add-Migration InitialCreate -StartupProject 你的项目名称

2. 改用.NET CLI执行迁移

绕开Visual Studio的COM交互,直接使用命令行工具操作:

# 分项目场景
dotnet ef migrations add InitialCreate --project 你的数据项目名称 --startup-project 你的Web项目名称

# 单项目场景
dotnet ef migrations add InitialCreate

3. 修复Visual Studio环境

  • 关闭Visual Studio,删除项目根目录下的隐藏.vs文件夹,重新打开项目。
  • 通过Visual Studio Installer选择当前VS版本,点击「修改」-「修复」,修复可能损坏的Shell组件。

4. 匹配EF Core与ASP.NET Core版本

确保项目中Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Tools等NuGet包版本为3.1.x,与ASP.NET Core 3.1版本完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 23:16:00