Efcore: CSharpEntityTypeGenerator not available.

Created on 21 Oct 2019  路  2Comments  路  Source: dotnet/efcore

Project is unable to find CSharpEntityTypeGenerator.

Steps to reproduce

I've added Microsoft.EntityFrameworkCore.Design via NuGet, and in my .csproj file I have this:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EPPlus" Version="4.5.3.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0-preview1.19506.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0-preview1.19506.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0-preview1.19506.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
    <PackageReference Include="System.DirectoryServices" Version="4.7.0-preview1.19504.10" />
    <PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.7.0-preview1.19504.10" />
  </ItemGroup>

</Project>

Then, in my code, I'm trying to do this:

``` C#
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;

nullable enable

pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

// https://stackoverflow.com/questions/40728223/entity-framework-core-customize-scaffolding

namespace LaMP
{
public class EntityTypeGenerator : CSharpEntityTypeGenerator
{
public EntityTypeGenerator(ICSharpHelper helper) : base(helper) { }

    public override string WriteCode(IEntityType type, string @namespace, bool useDataAnnotations)
    {
        var code = base.WriteCode(type, @namespace, useDataAnnotations);

        var old = "public partial class " + type.Name;
        var updated = "[System.CodeDom.Compiler.GeneratedCode]\n" + old;

        return code.Replace(old, updated).Replace("namespace", "#nullable disable\n\nnamespace");
    }
}

public class MyDesignTimeServices: IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection servicesCollection)
    {
        servicesCollection.AddSingleton<ICSharpEntityTypeGenerator, EntityTypeGenerator>();
    }
}

public class ContextTypeGenerator: CSharpDbContextGenerator

}
```

Visual studio doesn't like CSharpEntityTypeGenerator or ICSharpHelper, and tells me I need to install Microsoft.EntityFrameworkCore.Design even though I've already done that.

Further technical details

EF Core version: Not sure how to tell
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.4

closed-question customer-reported

Most helpful comment

Remove IncludeAssets and PrivateAssets from the PackageReference

All 2 comments

Remove IncludeAssets and PrivateAssets from the PackageReference

Thank you!

Was this page helpful?
0 / 5 - 0 ratings