Deck by Eamonn Boyle (INSTIL) for the Belfast .NET Meetup
© Instil Software 2017January 2017[email protected][email protected][email protected]Entity Framework CoreBelfast .NET Developer Guild
View Slide
© Instil Software 2017• Software Trainer at Instil• Since 5 months ago• Spent previous years mainly on .NETand C++• Working with hardware• Performance• Low level• Interested in all things techAbout MeDisclaimer – Not a DBA or EntityGuru
© Instil Software 2017GoalsIntroduce Entity Framework Core• It’s the Core part I’m interested in• Assumes no previous knowledge of Entity FrameworkWhat it’s NotMasterclass on Entity FrameworkOverview
© Instil Software 2017• .NET Platform – the new kid• Open Source• Cross Platform• Can deploy within the application• Complies with .NET StandardNew and still evolving.NET Core Refresher
© Instil Software 2017.NET Platforms
© Instil Software 2017.NET Standard
© Instil Software 2017Most Import Question - S.Q.L. or Sequelhttp://www.vertabelo.com/blog/notes-from-the-lab/sql-or-sequel
© Instil Software 2017Microsoft’s recommended Object Relational Mapper• Allows developers to interact with relational databases• Use domain specific classes and LINQ – translated to SQL• Combat Object-relational impedance mismatchSupports different workflows• Code or Box & Line designer• Database or Code or Model firstRelies on .NET Framework• Current version – EF 6.xWhat is Entity Framework“Entity Framework is Microsoft’s recommendeddata access technology for new applications”MSDN
© Instil Software 2017What is Entity FrameworkCode Model SQL DB
© Instil Software 2017A complete rewrite of Entity Framework• Cross platform focus – targets .NET Standard• Lightwieght• ExtensibleStarted life as EF 7• Changed name to clarify that it is a different product• Does not have all features of EF 6Supported on .NET Framework, .NET Core and Xamarin*• Current version is EF Core 1.1.0*Eventually, when it supports .NET Standard 2.0What is Entity Framework Core
© Instil Software 2017Timeline2008 – EF 1.0 – First version2010 – EF 4.0 - Second version – not semantic versioning2011 – EF 4.1 – Code First support2013 – EF 6.0June 2016 – EF Core 1.0November 2016 – EF Core 1.1.0
© Instil Software 2017• Uses the same approach to provide data access• Largely the same developer experience• APIs largely the same• Entity classes, DbSet, DbContext etc• Use LINQ and automatically translated to queries• Supports .NET FrameworkEF Core vs EF 6EF Core EF 6New productCross platform (.NET and OS)Explicit configuration and DISome EF Core Only FeaturesStable and mature.NET Framework (Windows)Auto-configuration MagicSome EF 6 Only Features
© Instil Software 2017EF Core – Missing Featureshttps://docs.microsoft.com/en-us/ef/efcore-and-ef6/features• Complex/value types• Visualising Model, Wizard for Reverse Engineering• Simple Type Conversions• Spatial data types• Many to Many without join• TPT and TPC. Only TPH Supported• Improved Translation - GroupBy• Lazy Loading• Raw SQL for non-model types• Lifecycle Hooks - Simple Command Interception• Explicit Loading• Connection Resiliency
© Instil Software 2017EF Core – Unique Features• Shadow State Properties• Alternate Keys• Key Generation - Client• Reverse Engineering from Command Line• Better “Pretty” SQL Generation• Mixed Client Server Evaluation• Raw SQL Composing with LINQ• Batching of Statements• Detached Graph Support• InMemory Database Provider• Support in .NET Core, UWP, Xamarin etchttps://docs.microsoft.com/en-us/ef/efcore-and-ef6/features
© Instil Software 2017Database ProvidersDatabase Maintained UnderMicrosoft SQL Server (including SQL Azure) EFSQLLite EFIn Memory EFOracle under EvaluationNot yet supportedEFPostgreSQL NpgsqlMySQL MySQLPomeloSapient GuardianDevart dbConnectMySQL, Oracle, PostgreSQL, SQLite, DB2, Cloud appsDevart dbConnectSQL Server Compact Edition ErikEJ/EntityFramework.SqlServerCompact
© Instil Software 2017• LegoEntities - .NET StandardSetup Provider/ContextLoggerDemo
© Instil Software 2017The toolchain I’m using is,• Visual Studio 2017 RC 1• .Net Core 1.1 – MSBuild Tools• Much simplified csproj project formatWhy has Microsoft gone back to csproj from project.json file• One .NET tools ecosystem• Project to project references• Proven scalabilityThe CLI works the sameCSProj is Back, Baby!
© Instil Software 2017No major “regular” EF releases• Only bug fixes, community contributions etcAll new major features & innovations are going into EF Core• Azure Table Storage• Redis• Non relational databases• UWP and XamarinWhat’s in the Future
© Instil Software 2017If targeting .NET Core, then you have no choiceOtherwise, it depends• Can you survive without the missing features?• Can you survive with some instability?• Do you have use cases that need a performance gain?Avoid migrating existing code from EF 6.x to EF Core• Advantages don’t outweigh the effortShould I use EF Core?
© Instil Software 2017Entity Framework Corehttps://docs.microsoft.com/en-us/ef/core/EF6 vs EF Core 1.0 Feature Comparisonhttps://docs.microsoft.com/en-us/ef/efcore-and-ef6/features.NET Corehttps://www.microsoft.com/net/core#windowsvs2015Visual Studio 2017 RChttps://www.visualstudio.com/vs/visual-studio-2017-rc/Lego Data Setshttp://rebrickable.com/downloadsMore Info
© Instil Software 2017Appendix
© Instil Software 2017Raw SQL Examplevar blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs").ToList();var blogs = context.Blogs.FromSql("EXECUTE dbo.GetMostPopularBlogs").ToList();var user = "johndoe";var blogs = context.Blogs.FromSql("EXECUTE dbo.GetMostPopularBlogsForUser {0}", user).ToList();
© Instil Software 2017Raw SQL and then Compose with LINQvar searchTerm = ".NET";var blogs = context.Blogs.FromSql("SELECT * FROM dbo.SearchBlogs {0}", searchTerm).Where(b => b.Rating > 3).OrderByDescending(b => b.Rating).ToList();
© Instil Software 2017Centos Setup1. sudo yum install libunwind libicu2. curl -sSL -o dotnet.tar.gzhttps://go.microsoft.com/fwlink/?linkid=8379693. sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz-C /opt/dotnet4. sudo ln -s /opt/dotnet/dotnet /usr/local/binhttps://github.com/dotnet/core/blob/master/release-notes/preview4-download.md
© Instil Software 2017MySQL EF Core Providerhttps://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/master/README.md#getting-started