Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

c# - EF Core 1.1 Migration - The current CSharpHelper cannot scaffold literals of type

This seems to be a really obscure error, and I don't even know where to start with it...

The current CSharpHelper cannot scaffold literals of type 'System.Func`3[Microsoft.EntityFrameworkCore.Metadata.IProperty,Microsoft.EntityFrameworkCore.Metadata.IEntityType,Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator]'. Configure your services to use one that can.

This is occurring when I run

dotnet ef migrations add initial

Update 1 - based on Rob's question

The architecture of the application uses a rich domain model, so there are a few "workarounds" to get things like encapsulated collections, and Jimmy Bogard's typed enumeration pattern working.

Each domain model object has it's own EntityTypeBuilder class in the data access layer, all of which are called from OnModelCreating in the DbContext. These also output some information to the Console, so I can see that the builder isn't causing any errors (none of them are at this point).

The DbContext only exposes DbSet for aggregate root entities, or to put it another way, only entities that have a corresponding repository class. The rest of the domain model objects are navigation properties, and therefore do not require DbSet to be exposed via the DbContext.

Platform specifics (as far as I'm aware)

  • .NET Core 1.1 (Runtime + SDK)
  • EF Core 1.1
  • Microsoft.EntityFrameworkCore.Tools 1.0.0-preview2-final
  • Microsoft.EntityFrameworkCore.Design 1.0.0-preview2-final
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using a .NET Core 1.1 runtime but the old tooling packages (you should be using the latest tooling, as of this date it is preview4, which is still in alpha, contrary to the runtime - yeah, not very intuitive at first)

In summary, this is a Known Issue

"tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
}
  • You may, or may not need to fix the SDK version by using a global.json just above the src folder which should contain your code as explained in Hanselman's blog

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-1-003177"
  }
}

EDIT: Just to give you a more complete answer, this is how my project.json and my sample project looks like (all done in VS Code):

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.0-preview1-final",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0-preview1-final",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0-preview1-final",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0-preview1-final",
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final"
  }
}

VS Code Project Layout


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...