Guidance: How to use EF6 with ASP.NET Core 1 RC2 web app?
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!
@julielerman has an article in the latest MSDN magazine on how to do that!
in project.json:
"dependencies": {
"EntityFramework": "6.1.3"
},
"frameworks": {
"dnx451": {
"frameworkAssemblies": {
"System.Data": "4.0.0.0"
}
} //, "dnxcore50": { }
}
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 )
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.~~
@mundi4 Just saw your post - that's exactly what I did. Thanks.
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 https://github.com/aspnet/Identity/issues/416.
@rowanmiller Thanks. I was just about to test it out. I'll edit my post above.
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?
@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 https://github.com/aspnet/dnx/issues/952.
@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.
@rowanmiller The things that I believe are blocking me from porting my MVC5, EF6 content management system to EF7 are:
- Custom Convensions:
public class UnmapCoreEntitiesConvention : Convention
{
public UnmapCoreEntitiesConvention()
{
Types()
.Where( t => t.CustomAttributes.Any( n => n.AttributeType == typeof( CoreContentEntity ) ) )
.Configure( config => config.Ignore() );
}
}
- 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>();
}
- 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.
@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.
@rowanmiller Thanks. I'll try the manual install later today.
@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.
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.
@ToddThomson
Regarding the features you mentioned...
- 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);
}
}
}
- 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.
- 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.
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.
@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.
@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.
@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).