2

I am writing a .NET Framework WPF application and I use Entity Framework. At first I installed the EF Core package and when doing my first add-migration I received the famous error

Unable to create an object of type 'DbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

However, this is not what my question is about. I knew I got it to work sometime before and I noticed that that was when I used EF 6 instead of EF Core. So I uninstalled the EF Core packages and installed EF 6 package instead. And now with EF 6 migrating works just fine, which is great.

But my question is: why IS it working? Even though I did not change my code by adding an IDesignTimeFactory<DbContext> or dependency injection for instantiation of my DbContext class. Yet it still works just by switching back to EF 6. I couldn't find any information on differences between the two (i.e. EF Core and EF 6) that would explain this. So now I'm hoping someone here can enlighten me with their vast knowledge.

For reference, here is my DbContext class:

public class PinboardContext : DbContext
{
    public PinboardContext() : base()
    {
    }

    public DbSet<Item> Items { get; set; }
}

My App.xaml.cs class is empty apart from an empty constructor.

Lastly, if that is of importance, I have a connectionString defined in app.config as follows:

  <connectionStrings>
      <add name="mpcn" connectionString="Data Source=(localdb)\MSSQLLocalDB;  Initial Catalog=MyPinboardDB;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

Note that I have already looked at this but it didn't help me answer my question.

3
  • These are completely different frameworks, but with similarly named classes / methods. And you can't switch between the two without code changes - at least in EF6 you need using System.Data.Entity; and in EF Core - using Microsoft.EntityFrameworkCore;. The fact that it is working with EF6 means the code never worked with EF Core. Commented Apr 11, 2020 at 10:46
  • @IvanStoev Yes it never worked with EF Core - which is what my question was all about: why didn't it work with EF Core when it works with EF 6? The two seem to be completely different, yet people often just refer to "Entity Framework" without specifying which one exactly they are talking about. This has caused me a lot of trouble and wasted hours being new to EF Commented Apr 11, 2020 at 11:16
  • 1
    You should have started from Entity Framework Documentation. As you can see, it contains 4 root links - "Entity Framework Core", "Entity Framework 6", "Choosing" and "Port to EF Core". The last starts with "Because of the fundamental changes in EF Core we do not recommend attempting to move an EF6 application to EF Core unless you have a compelling reason to make the change. You should view the move from EF6 to EF Core as a port rather than an upgrade." Commented Apr 14, 2020 at 8:33

1 Answer 1

1

Probably because you have to pass a DbContextOptions<PinboardContext> to your DbContext base class.

If no options are provided, EF6 uses a connection string from the config file with the name of the derived DbContext, or failing that assumes a database with the name of the derived DbContext on the default database installation.

EF Core requires that options must be provided.

It is not a requirement in EF Core that the DbContext is created by injection. If it is created by injection, it must be configured with the appropriate DbContextOptions<T>. If created directly, the DbContextOptions<T> must be provided directly.

Sign up to request clarification or add additional context in comments.

10 Comments

Please elaborate. My question was why IS it working. So I don't think I have to pass anything anymore. If you mean when using EF Core I have to do this, then my question is why? What does EF Core do differently than EF 6 that necessitates this?
I've elaborated in the answer.
Thanks. Even when I provided options (incl. connectionString) with EF Core via the constructor, the error did not disappear. As far as I understand it EF Core needs the DbContext class to be provided via Dependency Injection. Regarding your statement "EF6 assumes a database with the name of the derived DbContext", I do not have a database with the name of the derived DbContext. The name of the connection string I specified is "mpcn" not "PinboardContext" and no database was existent prior to adding migration. I will add my connection string definition in App.config to my OP to make that clear.
For reference about the necessity of DI for EF Core, I am getting this from the top answer here github.com/dotnet-architecture/eShopOnContainers/issues/1080
@csstudent1418 I just noticed you're change of title on the question. I think the key point is, the differences are not subtle - they are fundamental.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.