Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guidance: How to use EF6 with ASP.NET Core 1 RC2 web app? #2336

Closed
ToddThomson opened this issue Jun 5, 2015 · 69 comments
Closed

Guidance: How to use EF6 with ASP.NET Core 1 RC2 web app? #2336

ToddThomson opened this issue Jun 5, 2015 · 69 comments
Assignees
Milestone

Comments

@ToddThomson
Copy link

I've been moving a subset of my MVC 5, EF6 application to ASP.NET 5, MVC 6 and EF7 and I really like the direction that the platform is moving to. Moving to EF7, however, poses many issues for me. It is just too short on required features that I have used in EF6. I feel I will be investing a great deal of time to add workarounds and to deal with the limitations.
So, what is the guidance for using EF6 with an ASP.NET 5 application? I have my database services and repository code in separate libraries (I have essentially a dll for each derived type + a dll for a base type ).
Any help would be very much appreciated!

@ErikEJ
Copy link
Contributor

ErikEJ commented Jun 5, 2015

@julielerman has an article in the latest MSDN magazine on how to do that!

@mundi4
Copy link

mundi4 commented Jun 5, 2015

in project.json:

    "dependencies": {
        "EntityFramework": "6.1.3"
    },
    "frameworks": {
        "dnx451": {
            "frameworkAssemblies": {
                "System.Data": "4.0.0.0"
            }
        } //, "dnxcore50": { }
    }

@julielerman
Copy link

Here's a link to the article: https://msdn.microsoft.com/en-us/magazine/dn973011.aspx.

Separation of Concerns is your friend here. Just have your data access (EF) stuff with EF6 in its own project (or projects if you ahve multiple models).

HTH (& thanks @ErikEJ )

@ToddThomson
Copy link
Author

Thanks @ErikEJ and @julielerman ! I've just finished reading through your article. It cleared up a bit of confusion on the project.json use of the "frameworks" property. I really have no need for dnxcore50 at the moment so I've just removed that framework.
I want to point out that I need a dependency on MVC 6 and a few other ASP.NET 5 bits from within my ASP.NET 5 class library as well the EF6 dependency. So I just added "EntityFramework": "6.1.3" into the project.json. This seems to work. I am not sure about migrations and using EF6 with visual studio 2015 RC, but hopefully that will work too.
Lastly, by keeping your service and repository code in separate class libraries it should be possible to use EF7 and Identity 3 as long as it too is walled in a separate library.

@ToddThomson
Copy link
Author

@mundi4 Just saw your post - that's exactly what I did. Thanks.

@rowanmiller
Copy link
Contributor

Lastly, by keeping your service and repository code in separate class libraries it should be possible to use EF7 and Identity 3 as long as it too is walled in a separate library.

I don't think that is true because you can't have two different versions of the same assembly loaded in an AppDomain. BTW there is some discussion on an EF6 provider for ASP.NET 5 Identity which would solve this issue aspnet/Identity#416.

@ToddThomson
Copy link
Author

@rowanmiller Thanks. I was just about to test it out. I'll edit my post above.

@ToddThomson
Copy link
Author

I've determined that the powershell scripts to enable migrations (etc) in the EF6 6.1.3 nuget package are not installed when the web application project type is the new ASP.NET 5 XPROJ type. I can understand the reasoning behind this. However, as I mentioned in the starting post for this issue, I cannot use EF7 due to its lack of features compared to EF6. Is there a chance that the DNX team and the EF team can come together to provide ASP.NET 5 XPROJ + EF6 migrations support?

@rowanmiller
Copy link
Contributor

@ToddThomson at this stage the ASP.NET 5 xproj system does not process the PowerShell files that can be included in a NuGet package. If you want to add your vote toward this feature then here is the place to do so aspnet/dnx#952.

@rowanmiller
Copy link
Contributor

@ToddThomson

I cannot use EF7 due to its lack of features compared to EF6

What features in particular are blocking you from using EF7? I'd like to understand if they are things that will be implemented by the initial RTM of ASP.NET 5 and EF7 or whether they are things that will still be missing once we move beyond pre-release.

@ToddThomson
Copy link
Author

@rowanmiller The things that I believe are blocking me from porting my MVC5, EF6 content management system to EF7 are:

  1. Custom Convensions:
public class UnmapCoreEntitiesConvention : Convention
    {
        public UnmapCoreEntitiesConvention()
        {
            Types()
                .Where( t => t.CustomAttributes.Any( n => n.AttributeType == typeof( CoreContentEntity ) ) )
                .Configure( config => config.Ignore() );
        }
    }
  1. Many to Many
protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
            modelBuilder.Entity<ContentBase>()
                .HasMany( e => e.Tags )
                .WithMany( e => e.ContentBase )
                .Map( m => m.ToTable( "cms_ContentBaseTags" ).MapLeftKey( "ContentId" ).MapRightKey( "TagId" ) );

            // Remove all contentBase derived types

            modelBuilder.Conventions.Add<UnmapCoreEntitiesConvention>();
        }
  1. TPT Inheritance: In my CMS, every content type inherits from ContentBase ( all content types along with their services and repositories are in separate dlls )

I hope that helps.

@rowanmiller
Copy link
Contributor

@ToddThomson I'll come back to the features in the next couple of days 1 and 2 are possible but the code looks a little different. 3 is not possible at the moment.

I've determined that the powershell scripts to enable migrations (etc) in the EF6 6.1.3 nuget package are not installed when the web application project type is the new ASP.NET 5 XPROJ type

I think you will be able to manually run Init.ps1 from the EntityFramework NuGet package (in the packages folder) from Package Manager Console to load the commands. I haven't tried it yet, so I'm not sure if it works... thought I'd mention it in case you want to experiment with it before I get a change to.

@ToddThomson
Copy link
Author

@rowanmiller Thanks. I'll try the manual install later today.

@ToddThomson
Copy link
Author

@rowanmiller A simple way to get EF6 commands to work in an ASP.NET 5 XPROJ solution is to add an empty CPROJ project and add EF6 6.1.3 nuget package to both. At that point you can run enable-migrations, etc ( EF6 commands ).
When targeting the ASP.NET 5 project, the commands fail with:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At C:\src\temp\WebApplication5\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:730 char:5
+     $domain.SetData('project', $project)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SerializationException

The code at that location is:

    $domain = [AppDomain]::CreateDomain('Migrations', $null, $info)

    $domain.SetData('project', $project)
    $domain.SetData('contextProject', $contextProject)

I'll investigate a bit more, but it looks like a dead end.

@rowanmiller rowanmiller added this to the Discussions milestone Jun 12, 2015
@rowanmiller rowanmiller self-assigned this Jun 12, 2015
@rowanmiller
Copy link
Contributor

Looping in @cherrydev as I am told they have already looked at enabling migrations in EF6/ASP.NET5 scenarios. They may be able to share some of their finding here.

@rowanmiller
Copy link
Contributor

@ToddThomson

Regarding the features you mentioned...

  1. Custom Conventions

We may introduce an API similar to what we had in EF6. But because we changed the model builder to construct the model as it goes (rather than at the end based on the config you supplied) you can query the model and configure based on it's current shape.

i.e. this code achieves what your sample did on EF6

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var type in modelBuilder.Model.EntityTypes)
            {
                if(type.ClrType.CustomAttributes.Any(n => n.AttributeType == typeof(CoreContentEntity)))
                {
                    modelBuilder.Ignore(type.ClrType);
                }
            }
        }
  1. Many to Many

You can still model the many:many pattern, you just need to have the join entity be part of your entity model (i.e. you would have a ContentBaseTag entity). In the future we will probably just allow you to put this entity in shadow state, so the join entity is still part of your model... you just don't have to have a CLR class for it.

  1. TPT

Correct that this isn't supported yet. If modifying the core table was a possibility then we are introducing support for TPH at the moment.

@yashvit
Copy link

yashvit commented Jul 2, 2015

Are we looking at a production release of EF7 along with VS2015? I'm looking at migrating an existing production MVC4 + EF6 project to vNext. If EF7 isn't coming out soon and EF 6 isn't fully compatible with new VS then I might as well wait a little longer before I migrate.

@rowanmiller
Copy link
Contributor

@yashvitnaik EF7 will become RTM at the same time as ASP.NET 5 (MVC6). Neither of these will be at the same time as VS2015 RTM (both will still be preview in the VS2015 RTM). The ASP.NET team is working on making their dates public.

@ToddThomson
Copy link
Author

@rowanmiller TPT is a must have for me to use EF7. I've investigated the alternatives, but I can't make TPH or TPC work.
Thank-you for guidance concerning 1 and 2 above.

I'll come back to using EF6 and ASP.NET 5 when VS 2015 goes RTM. My hope is that EF6 migration commands will work and that there will be an identity 3 with an EF6 component dependency.

@rowanmiller
Copy link
Contributor

@ToddThomson we actually just took a series of package/assembly renames so you will be able to have EF6/EF7 running in the same app (so you can use Identity 3 and still use EF6 for your data access).

@ToddThomson
Copy link
Author

@rowanmiller Perfect. Is TPT on the radar for EF7?

Edit: This is all good, but vs2015 not being able to run EF6 migration commands will be trouble ( which is the case right now ).

@rowanmiller
Copy link
Contributor

@ToddThomson not for the initial RTM. We will evaluate after that to see what we want to support beyond TPH for EF7.

@ToddThomson
Copy link
Author

@rowanmiller For SxS scenario, I can have a VS2015 ASP.NET 5 xproj which has dependencies on EF6 and EF7. The problem is that the power shell scripts for EF6 ( migrations ) only work with csproj projects. Are the EF6 migrations going to be updated to support vs2015 ASP.NET 5 xproj type?

@ToddThomson
Copy link
Author

Perhaps dnx . ef6 ...
EntityFramework6.Commands for xproj type.

@cherrydev
Copy link

I have EF6 migration commands working in DNX but I've repeatedly asked for someone with commit access in ASPNET for help actually getting them into a public repository and haven't yet gotten a solid response.

@ToddThomson
Copy link
Author

@cherrydev Given the recent changes to have EF6 and EF7 SxS, hopefully the ASP.NET 5 team will be more receptive to EF6 migrations working with DNX.
Do you have a GitHub repository for your EF6 DNX migration commands?

@rowanmiller
Copy link
Contributor

@ToddThomson @cherrydev xproj is going to support loading commands in Package Manager Console, so the EF6 commands will start working.

@ToddThomson
Copy link
Author

@rowanmiller Thanks. Is the xproj support for EF6 commands in vs2015RTM?

@rowanmiller
Copy link
Contributor

@ToddThomson we did rename the EF7 EntityFramework.SqlServer assembly/package to EntityFramework.MicrosoftSqlServer so that you will be able to use EF6/EF7 side-by-side in the same app. This wasn't in Beta 8 though, so you'd need to use a nightly build or wait for RC1 to do this.

We are working on Init.ps1 support... but I don't have an exact time that it will light up (before RTM, but I'm not exactly sure what release).

@ToddThomson
Copy link
Author

@rowanmiller Good decision. I can now move to Identity 3 and what I can to EF7 and wait for TPT/TPC. Thank-you for the update!

@ghrapan
Copy link

ghrapan commented Nov 27, 2015

Hi guys!

In case somebody is looking for an example of how to make Identity 3.0 to utilize EF6 instead of EF7, I have published my implementation here:

https://github.com/entrypointsoft/AspNet.Identity.EntityFramework6

@SharePointRadi
Copy link

@mrahhal
Copy link

mrahhal commented Mar 8, 2016

For anybody looking for a dnx based EF6 migrator, I've implemented one here: Migrator.EF6.

@matguthrie
Copy link

Hi,

I'm encountering the same "Exception calling "SetData" with "2" argument(s)" issue when calling add-migration. I have a separate data project using EF 6.1.3 and have been using migrations successfully with a ASP.NET 4.5 project. I've just added a ASP.NET 5 web app to the solution and I'm now unable to add a migration. The target project is the data project itself.

If I remove the ASP.NET 5 project from the solution I can add the migration without issue, although adding and removing the project every time i want to update the DB is not ideal

Any suggestions?

Mat

@mrahhal
Copy link

mrahhal commented Apr 17, 2016

@matguthrie If you're talking about Migrator.EF6 (I can't figure out from the context) please open an issue in that repository.

@ToddThomson ToddThomson changed the title Guidance: How to use EF6 with ASP.NET 5 application? Guidance: How to use EF6 with ASP.NET Core 1 RC2 web app? May 11, 2016
@ToddThomson
Copy link
Author

@rowanmiller Just thought I'd review this question for the upcoming ASP.NET Core 1 RC2 release which I plan to test out next week.

Will EF Core 1 RC2 and EF6 still work together in the same web project?
Will there be any tooling for migrations for EF6 in VS2015 preview 1 tooling support? Are migrations supported in VS Code?
Is there any likelihood that TPT will be provided in EF Core 1 this year?

My web app targets the full framework, and I require TPT so EF6 is my only option for now.

Thanks in advance!

@rowanmiller
Copy link
Contributor

@ToddThomson The state in RC2 will be the same as RC1. You can use EF6/EF Core side-by-side, but there is no xproj compatible migrations tooling for EF6. In VS Code you would use the dotnet ef ... command line tooling for migrations (EF Core only).

@mrahhal
Copy link

mrahhal commented May 12, 2016

@ToddThomson the same way Migrator.EF6 has been done to work with migrations in dnx, a cli tool can be created for EF6 migrations to work in RC2 as well.

And since the state will be the same, we'll still be able to use all features of EF6 in RC2.

@mrahhal
Copy link

mrahhal commented May 19, 2016

For anyone who found this useful in dnx, Migrator.EF6 now supports RC2 as a .NET Core CLI tool (much like how the EF Core migration tool works).

@aman-saggu-git
Copy link

Hi Everybody , I have a question if I use below Code Then I think I will be using Entity Framework Not (.Net Core ) or I am making mistake

{ "dependencies": { "EntityFramework": "6.1.3" }, "frameworks": { "net461": { } } }

@rowanmiller
Copy link
Contributor

@bhallaheemanshu correct, that would be using EF6.x in an app that targets full .NET.

@aman-saggu-git
Copy link

@rowanmiller Can we achive scaffolding for asp.net core with Ef core

@rowanmiller
Copy link
Contributor

@bhallaheemanshu are you asking about scaffolding an MVC controller and views from and EF Core model? If so, then yes, that is supported.

@Eisenstein
Copy link

@mrahhal I'm getting an error 'No executable found matching command "dotnet-ef"'
What could be wrong?

@mrahhal
Copy link

mrahhal commented Jul 14, 2016

@Eisenstein please check the repo's issues (there's a good chance you'll find it) or create a new one.

@aman-saggu-git
Copy link

@rowanmiller any link suggestion to use scaffold & Migrations in rc2

@marchy
Copy link

marchy commented Jul 26, 2016

@mrahhal We get the following error when running any of the migration commands:
Value cannot be null.
Parameter name: type

Any idea how to trouleshoot this? Our setup runs fine at runtime (ASP.NET Core app) but the migration commands don't like it

UPDATE(solved): We had "emitEntryPoint" set to false instead of true in project.json. This fixed it!

@Bruce247
Copy link

Hi all.
I am also getting a 'No executable found matching command "dotnet-ef"' error when attempting to run "dotnet ef migrations add InitialDatabase" at the command prompt. I am running the command in the same folder as project.json, and ""emitEntryPoint": true,".
What else can I try???

@mrahhal
Copy link

mrahhal commented Oct 14, 2016

@Bruce247 if it's a problem with Migrator.EF6 please check the issues on that repo first and then create an issue if you still can't get it to work. In particular you should check mrahhal/Migrator.EF6#9.

@Bruce247
Copy link

@mrahhal
Thanks for your response. I'm afraid I have no idea if my issue is a 'migrator.EF6' one. I just don't have enough knowledge of these things right now. I tried suggestions at https://github.com/mrahhal /Migrator.EF6/issues/9 & https://github.com/mrahhal/Migrator.EF6/issues/9, but produced more errors than solutions, and had to revert it all.
I'm really just interested in finding the simplest way to get Shawn's code to work. A step-by-step for that would be wonderful! I fear I will have to abandon this course, which I really, really wanted to complete! :-)
Thanks again!
My (reverted) project.json code is as follows:
`{
"dependencies": {
"jQuery.Validation": "1.15.1",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
}
},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview2-final",
"imports": "portable-net45+win8+dnxcore50"
}
},
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
`

@mrahhal
Copy link

mrahhal commented Oct 14, 2016

@Bruce247 ah sorry about that. Looks like you're using EF Core, so you have nothing to do with Migrator.EF6. Hmm honestly not sure what's wrong here, your project.json looks good but there might be something I've missed.

@rowanmiller
Copy link
Contributor

Closing this issue as discussion has got very off track from the original "using EF6 in an ASP.NET Core app" topic.

@Bruce247 please open a new issue with the problem you are facing.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests