ErikEJ / EntityFramework6PowerTools

This is the codebase for Entity Framework 6 Power Tools Community Edition, and modern EF 6 providers for SQL Server and SQL Server Compact
Other
183 stars 27 forks source link

System.InvalidCastException #27

Closed gh5692 closed 6 years ago

gh5692 commented 6 years ago

I get the following error when using the context menu "Entity Framework"->"View Entity Data Model (Read-only)".

_System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{FFB2E715-7312-4B93-83D7-D37BCC561C90}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (ENOINTERFACE)). at Microsoft.DbContextPackage.Extensions.ProjectExtensions.GetProjectTypes(Project project) at Microsoft.DbContextPackage.Extensions.ProjectExtensions.IsWebProject(Project project) at Microsoft.DbContextPackage.DbContextPackage.GetUserConfig(Project project, String assemblyFullName) at Microsoft.DbContextPackage.DbContextPackage.DiscoverUserContextType(Type& systemContextType)

Using, VS 2017 Entity Framework 6.2.0 Target framework net47

ErikEJ commented 6 years ago

What type of project is that?

ErikEJ commented 6 years ago

Can you maybe share the project?

gh5692 commented 6 years ago

I'm not allowed to share the project but it's a .NET Framework 4.7 Class Library project.

ErikEJ commented 6 years ago

Can you show me the top of the csproj file?

gh5692 commented 6 years ago

Here is an excerpt of the csproj file.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SccLocalPath>.</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <OutputPath>bin\x86\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x64\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <OutputPath>bin\x64\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Dapper, Version=1.50.4.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\Dapper.1.50.4-alpha1-00070\lib\net451\Dapper.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="LinqKit, Version=1.1.9.0, Culture=neutral, PublicKeyToken=bc217f8844052a91, processorArchitecture=MSIL">
      <HintPath>..\packages\LinqKit.1.1.9.0\lib\net45\LinqKit.dll</HintPath>
    </Reference>
    <Reference Include="Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
      <HintPath>..\packages\Oracle.ManagedDataAccess.12.2.1100\lib\net40\Oracle.ManagedDataAccess.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
      <HintPath>..\packages\Oracle.ManagedDataAccess.EntityFramework.12.2.1100\lib\net45\Oracle.ManagedDataAccess.EntityFramework.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Security" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
ErikEJ commented 6 years ago

With just this package installed, I am unable to reproduce - what exact VS version are you using? I am using 15.4.4. I think your VS install may be broken...

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net47" />
</packages>
gh5692 commented 6 years ago

Microsoft Visual Studio Professional 2017 Version 15.4.5 VisualStudio.15.Release/15.4.5+27004.2010 Microsoft .NET Framework Version 4.7.02053

I'll run a repair and let you know the results.

ErikEJ commented 6 years ago

I do not have the guts to move to 15.4.5 ...

gh5692 commented 6 years ago

I created a new project and did a reverse engineer using the "ADO.NET Entity Data Model" "Code First from database" option. I don't get the error for this project so I'm going to hold off on the repair. I wonder if there is a conflict with another tool, a problem with the project or perhaps something with the code first classes causing the issue.

ErikEJ commented 6 years ago

Ok, closing for now

gh5692 commented 6 years ago

The issue was the project containing the context was not set as the StartUp project. If I change it to the startup project the error is not raised and the diagram opens as expected.

ErikEJ commented 6 years ago

Thanks for following up - nice to know!

HMoen commented 6 years ago

@gh5692 thanks for the tip on setting the library as the StartUp project. However, now I get the following error:

A constructible type deriving from DbContext could not be found in the selected file

The selected file is an abstract class that inherits from DbContext, but also derives from a custom interface. I commented out the custom interface and I still get the same error above.

I am using:

@ErikEJ, thank you for your time and a great tool.

ErikEJ commented 6 years ago

@HMoen Is an abstract class constructible?

HMoen commented 6 years ago

Doh, removed abstract keyword and added a parameter less ctor and it works beautifully.

Thanks @ErikEJ !

0xced commented 6 years ago

I had the same InvalidCastException, so I tried to set the project as startup project as suggested by @gh5692 and now I have an alert when I try to View Entity Data Model (Read-only) on my DbContext subclass : Sequence contains no matching element. Unfortunately, there's no stack trace with this alert. 😕

Visual Studio version 2017 15.6.6 EF Power Tools version 0.9.35

marcelopetersen commented 5 years ago

Hello,

I'm facing this error on Visual Studio 2019 version 16.2.1. After installed the extension, I receive the following error message when I try to generate views:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{FFB2E715-7312-4B93-83D7-D37BCC561C90}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). at Microsoft.DbContextPackage.Extensions.ProjectExtensions.GetProjectTypes(Project project) at Microsoft.DbContextPackage.Extensions.ProjectExtensions.IsWebProject(Project project) at Microsoft.DbContextPackage.DbContextPackage.GetUserConfig(Project project, String assemblyFullName) at Microsoft.DbContextPackage.DbContextPackage.DiscoverUserContextType(Type& systemContextType)

My project type is Class Library and is the startup project.

Any suggestion?

Regards

marcelopetersen commented 5 years ago

Hi Guys,

I think I found the solution: my solution has some class libraries projects and two mvc websites (api and web).

Looking to power tools source code, it accepts only two kinds of projects as follow:

https://github.com/ErikEJ/EntityFramework6PowerTools/blob/community/src/PowerTools/Extensions\ProjectExtensions.cs

` public const string WebApplicationProjectTypeGuid = "{349C5851-65DF-11DA-9384-00065B846F21}"; public const string WebSiteProjectTypeGuid = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}";

public static bool IsWebProject(this Project project) { DebugCheck.NotNull(project);

    return project.GetProjectTypes().Any(
             g => g.EqualsIgnoreCase(WebApplicationProjectTypeGuid)
               || g.EqualsIgnoreCase(WebSiteProjectTypeGuid));

}`

To make extension to work perfect, I set one of my web projects as Start up project, and success!

Rergards

cj-carroll commented 4 years ago

I am using vs2019, v16.3.7. My project that has the DbContext derived class is not the startup project. When I set it to the startup project, I still get the error.

Top of project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net47;net48</TargetFrameworks>
    <AssemblyName>DT.CustomerModel</AssemblyName>
    <PackageId>DT.CustomerModel</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
    <Platforms>AnyCPU;x64</Platforms>
  </PropertyGroup>

Getting the following error: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{FFB2E715-7312-4B93-83D7-D37BCC561C90}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). at Microsoft.DbContextPackage.Extensions.ProjectExtensions.GetProjectTypes(Project project) at Microsoft.DbContextPackage.Extensions.ProjectExtensions.IsWebProject(Project project) at Microsoft.DbContextPackage.DbContextPackage.GetUserConfig(Project project, String assemblyFullName) at Microsoft.DbContextPackage.DbContextPackage.DiscoverUserContextType(Type& systemContextType)

Any help is greatly appreciated.

ErikEJ commented 4 years ago

@cj-carroll Please dont post new issues in closed ones. Your issue is a duplicate of #62