{"id":996,"date":"2022-02-08T09:42:56","date_gmt":"2022-02-08T09:42:56","guid":{"rendered":"https:\/\/www.ignitingthought.com\/?p=996"},"modified":"2022-02-08T12:50:58","modified_gmt":"2022-02-08T12:50:58","slug":"a-complete-guide-on-entity-framework","status":"publish","type":"post","link":"https:\/\/stage.ignitingthought.com\/?p=996","title":{"rendered":"A Complete Guide on Entity Framework"},"content":{"rendered":"\n<p>Entity Framework is an Object Relational Mapping (ORM) framework that gives developers an automated way to sort and access data in databases. It eliminates the necessity for many of the data-access code that developers usually write<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why choose Entity Framework?<\/strong>&nbsp;<\/h2>\n\n\n\n<p>The purpose of EF is to allow you to deal with the data from relational databases using an object model that maps directly to your application&#8217;s business objects.&nbsp;<\/p>\n\n\n\n<p>By reducing the redundant task of persisting\u202fdata,\u202fentity\u202fframeworks\u202fhelp\u202fdevelopers increase their productivity.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Where does the Entity Framework fit?<\/strong>&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"315\" height=\"375\" src=\"https:\/\/www.ignitingthought.com\/wp-content\/uploads\/2022\/02\/layer.png\" alt=\"\" class=\"wp-image-997\" srcset=\"https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/layer.png 315w, https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/layer-252x300.png 252w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/><\/figure>\n\n\n\n<p>The Entity Framework fits basically between the database and the business logic layer. It\u2019s the data access layer where your entity framework fits, it will save the data stored in the properties of business entities, retrieves the data from the database, and convert it to business entities objects automatically.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Advantages of Entity Framework<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Easy to adopt.&nbsp;<\/li><li>Follow LINQ common query syntax.&nbsp;<\/li><li>Reduce development time.&nbsp;<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>Easy to adopt.&nbsp;<\/li><li>Follow LINQ common query syntax.&nbsp;<\/li><li>Reduce development time.&nbsp;<\/li><li>Reduce development costs.&nbsp;<\/li><li>Allows to query database in an object-oriented fashion.&nbsp;<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>EF6<\/strong><\/h3>\n\n\n\n<p>The newest version of the entity framework is EF6.&nbsp;<\/p>\n\n\n\n<p>EF6 is a mature and stable version of the entity framework that runs on the full .NET framework.&nbsp;<\/p>\n\n\n\n<p>Entity Framework 6 solves some issues with entity framework 5, which was a neighbourhood of the framework installed on the system and partly via NuGet extensions. Now the entire entity framework code is in NuGet packages.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"280\" src=\"https:\/\/www.ignitingthought.com\/wp-content\/uploads\/2022\/02\/version-history.png\" alt=\"\" class=\"wp-image-998\" srcset=\"https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/version-history.png 591w, https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/version-history-300x142.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>EF Core<\/strong>&nbsp;<\/h3>\n\n\n\n<p>Entity framework core is an open-source, lightweight, and extensible version of entity framework. It is the most recent version of Entity Framework following EF 6. x.&nbsp;<\/p>\n\n\n\n<p>The EF Core library is designed to be used with .NET Core applications, but it can also be used with any .NET 4.5+ framework-based application.&nbsp;<\/p>\n\n\n\n<p>\u202fEF Core supports LINQ queries, updates, schema migrations, and change tracking.&nbsp;<\/p>\n\n\n\n<p>Databases\u202fsuch\u202fas\u202fSQL Server\/<a href=\"https:\/\/www.ignitingthought.com\/introduction-to-azure-iot-smartness-redefined\/\" target=\"_blank\" rel=\"noreferrer noopener\">Azure<\/a> SQL\u202fDatabases, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and lots\u202fmore\u202fcan\u202fbe used with EF Core\u202fthrough\u202fits\u202fdatabase\u202fplugin model.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>EF Core Vs EF6<\/strong><\/h3>\n\n\n\n<p>EF Core is\u202fa\u202fversion of Microsoft Entity Framework for .NET Core applications\u202fthat\u202fprimarily\u202ftargets the code-first approach and provides\u202fminimal\u202fsupport for the database-first approach\u202fsince\u202fit\u202flacks a\u202fvisual designer or wizard for the\u202fdatabase\u202fmodel.&nbsp;<\/p>\n\n\n\n<p>EF 6 is\u202fa stable and mature ORM\u202fwhile EF Core is comparatively new. Microsoft has removed many of the internal dependencies and providers that EF 6 had after rewriting it from the ground up (like SQL client). At the\u202fend of the day,\u202fwhich will\u202fmake EF Core\u202ffar more\u202fextensible and lighter weight.&nbsp;<\/p>\n\n\n\n<p>Entity Framework Core offers a new feature in LINQ to Entities, that allows us to incorporate c# or vb.net functions in the query. In EF6, this was not possible.<\/p>\n\n\n\n<p>Entity Framework Core offers a new feature in LINQ to Entities, that allows us to incorporate c# or vb.net functions in the query. In EF6, this was not possible.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void Main (string &#91;] args) \r\n{ \r\n    var context = new LibraryContext(); \r\n    var authorsWithSameName = context.Library \r\n        . Where (l => l.FirstName == GetName()) \r\n        . ToList(); \r\n} \r\npublic static string GetName () { \r\n    return \"Amount\u201d; \r\n} <\/code><\/pre>\n\n\n\n<p>The GetName() function which is in the above linq query will excute the following in the database.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exec sp_executesql N'SELECT &#91;l].&#91;AuthorId], &#91;l].&#91;DoB], &#91;l].&#91;FirstName],  \r\n    &#91;l].&#91;BookId], &#91;l].&#91;LastName] \r\nFROM &#91;Library] AS &#91;l] \r\nWHERE &#91;l].&#91;FirstName] = @__GetName_0',N'@__GetName_0 nvarchar(4000)', \r\n    @__GetName_0=N'Amount' \r\nGo <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"229\" src=\"https:\/\/www.ignitingthought.com\/wp-content\/uploads\/2022\/02\/ef6.png\" alt=\"\" class=\"wp-image-999\" srcset=\"https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/ef6.png 602w, https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/02\/ef6-300x114.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Does\u202fEF6 or EF Core\u202fmake more sense?<\/strong><\/h3>\n\n\n\n<p>What Microsoft EF Core does is, it comes into the middle of everything and provides a feature called dB Context. Entity Framework Core is an extensible version of EF that has a very related capabilities and advantage to EF6. EF Core is a\u202ffully\u202frewritten\u202fversion of the Entity Framework\u202fand\u202fhas\u202fmany new features\u202fnot\u202ffound\u202fin\u202fEF6.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>Although there are many reasons to continue with Entity Framework 6, using Entity Framework with Windows universal platforms, using ASP.NET Core [1.0 to 5.0] on non-Windows-based platforms, and using non-relational data stores all need to be done with Entity Framework Core.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Entity Framework is an Object Relational Mapping (ORM) framework that gives developers an automated way to sort and access data in databases. It eliminates the necessity for many of the data-access code that developers usually write Why choose Entity Framework?&nbsp; The purpose of EF is to allow you to deal with the data from relational [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120],"tags":[],"class_list":["post-996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-solutions","entry"],"_links":{"self":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/996"}],"collection":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=996"}],"version-history":[{"count":8,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/996\/revisions"}],"predecessor-version":[{"id":1014,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/996\/revisions\/1014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/media\/1008"}],"wp:attachment":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}