39

Entity Framework Core does not support spatial data, which I need to use in my app.

Can I use Entity Framework 6 in .net core? If so, how can I register DatabaseContext in Startup.cs?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kostik
  • 639
  • 2
  • 10
  • 25

3 Answers3

39

Update

You can now use EF 6.3 with .NET Core 3.0:

https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/#what-s-new-in-ef-6-3

Below is an excerpt. However, EF Core has come a long way these days and it's worth giving it another go before going back to something that's reaching end-of-life soon. Specifically for your issue, EF Core supports mapping to spatial data types using the NetTopologySuite spatial library since version 2.2.

What’s new in EF 6.3

  • Support for .NET Core 3.0

  • The EntityFramework package now targets .NET Standard 2.1 in addition to .NET Framework 4.x.

  • This means that EF 6.3 is cross-platform and supported on other operating systems besides Windows, like Linux and macOS.

  • The migrations commands have been rewritten to execute out of process and work with SDK-style projects.

  • Support for SQL Server HierarchyId.

  • Improved compatibility with Roslyn and NuGet PackageReference.

  • Added ef6.exe utility for enabling, adding, scripting, and applying migrations from assemblies. This replaces migrate.exe. There are certain limitations when using EF 6.3 in .NET Core. For example:

  • Data providers need to be also ported to .NET Core. We only ported the SQL Server provider, which is included in the EF 6.3 package.

  • Spatial support won’t be enabled with SQL Server because the spatial types aren’t enabled to work with .NET Core.

  • Note that this limitation applies to EF 6.3 but not to EF Core 3.0. The latter continues to support spatial using the NetTopologySuite library.

  • There’s currently no support for using the EF designer directly on .NET Core or .NET Standard projects.

Original Answer

It isn’t ready yet, but starting with .NET Core 3.0, you will be able to.

https://blogs.msdn.microsoft.com/dotnet/2018/05/07/net-core-3-and-support-for-windows-desktop-applications/

Similarly, EF6 will be updated to work on .NET Core 3.0, to provide a simple path forward for existing applications using EF6.

https://youtu.be/GN54OV5cCBM?t=1146

But there's also EF6, which we've already announced is going to be ported to work on .NET Core...

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • 1
    You can already download a preview version: https://devblogs.microsoft.com/dotnet/announcing-entity-framework-6-3-preview-with-net-core-support/ – Jensdc Jun 03 '19 at 09:14
12

UPDATE: Yes, EF6 has been cross-platform since version 6.3. https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/#what-s-new-in-ef-6-3


ORIGINAL ANSWER THAT IS NOW INVALID: No, you can not directly, because EF6 doesn't support .NET Core. But, you can create another project, that compiles against full .NET framework and use it as a reference.

MS has actually made a decent tutorial for this: https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6#reference-full-framework-and-ef6-in-the-asp-net-core-project

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
Niko
  • 444
  • 3
  • 9
  • Can I use EF 6 DB first with asp.net core – Mahadev Sep 04 '18 at 12:43
  • 2
    If we do this, can we deploy the solution on a linux server ? – Terai Jan 03 '19 at 13:07
  • 1
    @Terai, no. From Docs (https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.2), under Overview section: To use Entity Framework 6, your project has to compile against .NET Framework, as Entity Framework 6 doesn't support .NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core. – kaushalparik27 Jan 24 '19 at 13:12
  • That Microsoft Document is utter rubbish. Spent hours trying to get it working but end of day the `Syste.Data.SqlClient` breaks everything in Core. The best interim solution is using `Z.EntityFramework.Classic.Community` (as per answers below) and then waiting for Core 3.0 and come back to EF6 ... if you need to. I dont think this should be the accepted answer.. anymore. But I dont want to downvote either.. please consider the other options. – Piotr Kula Mar 19 '19 at 15:27
  • Why is "Original answer no longer valid" ? Surely we can still create another project, that compiles against full .NET framework and use it as a reference. Then target Dotnet Core to run on full .NET Framework. – joedotnot Mar 07 '23 at 23:54
  • For anyone else wondering, "Original answer no longer valid" starting with .NET 3.0 and above. See here: https://github.com/aspnet/Announcements/issues/324 – joedotnot Mar 08 '23 at 03:16
8

UPDATE: Yes, EF6 has been cross-platform since version 6.3. https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/#what-s-new-in-ef-6-3


ORIGINAL ANSWER THAT IS NOW INVALID: Like @Niko said, you cannot directly with EF6 but there is a fork that allows it.

Disclaimer: I'm the owner of the project Entity Framework Classic

Entity Framework Classic is an EF6 fork. It's everything you like about EF6, but with better performance, must-have features, .NET Core support, and more.

There is a FREE Community version that includes pretty much everything and an Enterprise version that include additional features.

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60
  • 3
    Quite literally a drop in replacement.. Uninstall EF6, Install `Z.EntityFramework.Classic.Community` - If you getting `Unable to determine the provider name for provider factory of type` Make sure to install the same package in your root app, say AWS Lambda, Azure Function, Core Console app... – Piotr Kula Mar 19 '19 at 15:23
  • https://entityframeworkcore.com/providers-oracle seems to be very outdated? – Toolkit Oct 09 '19 at 10:16
  • 1
    I just added this to my project and it worked straight out of the box. Great work guys! – DeepToot Nov 29 '19 at 23:10
  • 2
    What about EDMX designer? – Flemming Bonde Kentved Dec 05 '19 at 17:13
  • Why is "Original answer no longer valid" ? Surely we can still create another project, that compiles against full .NET framework and use it as a reference. Then target Dotnet Core to run on full .NET Framework. – joedotnot Mar 07 '23 at 23:57
  • For anyone else wondering, "Original answer no longer valid" starting with .NET 3.0 and above. See here: https://github.com/aspnet/Announcements/issues/324 – joedotnot Mar 08 '23 at 03:15