EF 5.0 Enums Not Generating

2019-03-30 09:59发布

问题:

BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which I've read was an in-place install (overrode the .net 4.0 version).

I have projects still targeting 4.0 and 4.5 option is not available but was told it's ok since 4.5 was an in-place install. I then installed EntityFramework -pre via nuget and notices when I ran Upgrade-Database -Script commands, it would not generate enum properties.

I then found this. I tried doing everything from scratch again but it was still adding EntityFramework 4.4 instead of 5.0. So I manually changed all references to point to the 5.0 version to make sure I have EF 5.0 version. All compiled.

PROBLEM When I run

Enable-Migrations -EnableAutomaticMigrations

I get "No classes deriving from DbContext found in the current project. Edit the generated Configuration class to specify the context to enable migrations for."

So I manually made sure that my class is correct as in:

internal sealed class Configuration : DbMigrationsConfiguration<DataContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

DataContext subclasses DbContext.

When I run

Update-Database -Script

I get "No migrations configuration type was found in the assembly 'MyProject'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)."

MyProject does have the configuration class I mentioned above and in Package Manager Console I am choosign the right dropdown for my project containing Migrations folder and this Configuration class.

QUESTION

  1. What do I do to make sure when I install EnittyFramework via nuget that it adds the 5.0 version and not 4.4 even though I have .Net 4.5 installed?

  2. If I can't do anything related to the question above, what can I do to make sure Upgrade-Database spit out a script?

回答1:

Entity Framework 5.0 isn't out yet. There are prereleases, but you need to specifically enable prereleases in order for NuGet to display them.

However, keep in mind that EF 5.0 won't support everything in .NET 4.0 that it will in .NET 4.5. Yes, .NET 4.5 overwrites .NET 4.0, but if your project is in VS2010, it will be configured to build for .NET 4.0, not .NET 4.5. It cannot assume .NET 4.5 features, because the result needs to run on systems that don't have .NET 4.5. You're targeting .NET 4.0, after all, and enum support isn't in the EntityFramework DLL you get from NuGet, it's in the System.Data.Entity DLL that's part of the .NET runtime, so it cannot be added in .NET 4.0. You can install the Visual Studio Beta to create applications that target .NET 4.5.

In short: "was told it's ok since 4.5 was an in-place install" -- no, it's not ok for your purposes.