├── .gitattributes ├── .gitignore ├── ExampleBookSellingSite.png ├── ExampleBookSellingSiteStructure.png ├── LICENSE ├── README.md └── SoftwareStructure.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc 262 | /EfCoreInAction.sln.DotSettings 263 | /EfCoreInAction/Properties/PublishProfiles 264 | -------------------------------------------------------------------------------- /ExampleBookSellingSite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPSmith/EfCoreInAction/ab65567bc462f314c16790c6e46997d0779caa98/ExampleBookSellingSite.png -------------------------------------------------------------------------------- /ExampleBookSellingSiteStructure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPSmith/EfCoreInAction/ab65567bc462f314c16790c6e46997d0779caa98/ExampleBookSellingSiteStructure.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jon P Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EfCoreInAction 2 | 3 | Welcome to the Git repo that is associated with the book 4 | **[Entity Framework Core in Action](https://www.manning.com/books/entity-framework-core-in-action?a_aid=su4utaraxuTre8tuthup&a_bid=4cef27ce)** 5 | published by [Manning Publications](https://www.manning.com/). 6 | This book details how to use 7 | [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/index) (EF Core) 8 | to develop database access code in [.NET Core](https://www.microsoft.com/net) applications. 9 | 10 | This Git repo contains all the code in the book, plus an 11 | [example book selling site](http://efcoreinaction.com/) 12 | that I develop, and improve, in the book. 13 | All the code uses Microsoft's, open-source 14 | [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/index) library for database access. 15 | 16 | **NOTE: Code uses .NET Core 2.0, with a few branches converted to .NET Core 2.1** 17 | *See [Where's the code](#wheres-the-code).* 18 | 19 | To run the changed Git repo code you need to: 20 | 21 | 1. Install .NET Core 2.0 SDK – go to https://www.microsoft.com/net/download/core and select the correct SDK for your system 22 | 2. I recommend updating to Visual Studio 2017 15.3 or later as that has the new templates for ASP.NET Core 2.0.0. Or use Visual Studio code (the code is designed to work with either VS2017 or VSCode) 23 | 24 | ## Where's the code! 25 | 26 | This repo has a branch-per-chapter, and sometime and branch-per-section, approach 27 | so if you are looking at the master branch then you won't see any code! 28 | Just click the [Branches](https://github.com/JonPSmith/EfCoreInAction/branches) on the site and you will 29 | find all the branches I have created so far. 30 | 31 | ### Map of the branches 32 | 33 | The branches mainly inherit from each other (shown as `-->` in the diagram) 34 | but a few are on there own, like `master` and `Chapter01` (shown with spaces between them). 35 | The ones that are build for deployment to a web site are branches off the main stem (shown as higher offshoots). 36 | I did that because I built the normal code so you can run it locally without migrations. 37 | **Note: to fit the diagram in I shorten the real branch names and use `Ch4` instead of `Chapter04`** 38 | 39 | #### Part 1 - Getting started 40 | 41 | ```plaintext 42 | Ch5Migrate 43 | / 44 | master Ch1 Ch2 --> Ch3 --> Ch4 --> Ch5 --> 45 | ``` 46 | 47 | #### Part 2 - Entity Framework in depth 48 | 49 | ```plaintext 50 | (ch5) --> Ch6 --> Ch7 --> Ch8 --> Ch9 51 | ``` 52 | 53 | #### Part 3 - Using Entity Framework in real-world applications 54 | 55 | ```plaintext 56 | Ch13-Part3 57 | / 58 | Ch13-Part2 Ch14MySql 59 | / / 60 | (ch9) --> Ch10 --> Ch11 --> Ch12 --> Ch13-Part1 --> Ch14 --> (Ch15) Note1 61 | ``` 62 | *Note1: Chapter 15, on unit testing has its own Git repo https://github.com/JonPSmith/EfCore.TestSupport 63 | and a NuGet package called [EfCore.TestSupport](https://www.nuget.org/packages/EfCore.TestSupport/).* 64 | 65 | ### NET Core 2.1 examples 66 | 67 | I wanted to compare the performance of EF Core 2.0 and EF Core 2.1, so I created a few new branches. 68 | Here is the list: 69 | 70 | * Chapter05-NetCore21 71 | * Chapter13-Part1-NetCore21 72 | * Chapter13-Part2-NetCore21 73 | 74 | ### NET Core 3 examples 75 | 76 | To support EF Core 3 I created new branches. They are there to help you if you want to work with EF Core 3. 77 | I also used the **Chapter13-Part3-NetCore3** branch to find all the changes between EF Core 2 and EF Core 3 (the unit tests are very helpful for that). 78 | Here is the list: 79 | 80 | * Chapter01-NetCore3 81 | * Chapter05-NetCore3 82 | * Chapter13-Part3-NetCore3 83 | 84 | ## Live example book selling site 85 | 86 | You can find a live verison of the [example books selling site](http://efcoreinaction.com/). 87 | This provides an online example of the site to look at. You can't edit the data on the live site for obvious reasons, 88 | but you can if you clone the git this Git repo and run the example application locally. 89 | You can 'buy a book' though on the live site, and even see your old 'purchases'. 90 | 91 | The **Logs** navigation button will show you the SQL code that EF Core used to implement the last command you used. 92 | 93 | ![Diagram of site](https://github.com/JonPSmith/EfCoreInAction/blob/master/ExampleBookSellingSite.png) 94 | 95 | *Note: The admin button is missing on the live site.* 96 | 97 | ## Database naming conventions 98 | 99 | Because the database structure can change in each branch I use a system of building the database name 100 | using the branch name from a file which is written by a small gulpfile that is run by Visual Studio # 101 | on every build. 102 | 103 | Also I use [xUnit unit tests](https://xunit.github.io/) which run in parallel, so I cannot use one 104 | database for all the tests, as they would all be changing the database at one time. 105 | I therefore use another system to append the test class name, and sometimes the method that ran. 106 | **This is all explained in chapter 11**, but I wanted to warn you you will get lots of databases 107 | if you run the unit tests. 108 | 109 | If you get fed up with all databases there is a unit test in 110 | [test/UnitCommands/DeleteAllUnitTestDatabases](https://github.com/JonPSmith/EfCoreInAction/blob/Chapter02/Test/UnitCommands/DeleteAllUnitTestDatabases.cs) 111 | that will delete all the unit test databases in the branch you are in. 112 | 113 | ## Licence 114 | 115 | [MIT](https://github.com/JonPSmith/EfCoreInAction/blob/master/LICENSE) 116 | 117 | ## Other links 118 | My twitter handle is @thereformedprog. 119 | My blog site is at http://www.thereformedprogrammer.net/ where you will find articles on EF 6.x 120 | and some on EF Core. 121 | 122 | Happy coding! -------------------------------------------------------------------------------- /SoftwareStructure.md: -------------------------------------------------------------------------------- 1 | # The structure of the software 2 | 3 | This is an overview of the structure of the software in this GitHub repo. 4 | 5 | The repo is split up into branches, with each branch containing a working application. 6 | You need to select the branch in Visual Studio and `Pull` the branch you want to work with. 7 | 8 | You have three parts you can look at: 9 | 10 | 1. A console application that is fairly simple, and a good place to start if you are new to .NET 11 | 2. An ASP.NET Core application, which is much more complex and is meant to be similar to real-world applications. 12 | 3. A `Test` project, which contains numerous unit tests. 13 | 14 | Read the notes below for more details on each of these three. 15 | 16 | ## 1. Simple console application 17 | 18 | If you want a really simple application to play with then select the `Chapter01` branch. 19 | This contains a project called `MyFirstEfCoreApp`, which is a console application and 20 | fairly easy to run and/or change. You can see it in action in Chapter 1 of the book. 21 | 22 | The files to look at are: 23 | * [Program.cs](https://github.com/JonPSmith/EfCoreInAction/blob/Chapter01/MyFirstEfCoreApp/Program.cs) 24 | which has the code to read the commands that you type in. 25 | * [Commands.cs](https://github.com/JonPSmith/EfCoreInAction/blob/Chapter01/MyFirstEfCoreApp/Commands.cs) 26 | which contains each of the commands that the access the database. 27 | * [AppDbContext.cs](https://github.com/JonPSmith/EfCoreInAction/tree/Chapter01/MyFirstEfCoreApp) 28 | which contains the application's AppDbContext. 29 | * `Book.cs` and `Author.cs` contain the entity classes that I use with the database. 30 | * `MyLoggingProvider.cs` is used to log the SQL commands (which I show in chapter 1). 31 | 32 | ## 1. The ASP.NET Core application - an example book selling site 33 | 34 | This is a multi-project application. The figure below shows you the whole application after chapter 4 35 | (before chapter 4 then the `BizLogic` and `BizDbAccess` projects aren't there). 36 | 37 | ![Diagram of site](https://github.com/JonPSmith/EfCoreInAction/blob/master/ExampleBookSellingSiteStructure.png) 38 | 39 | I think the diagram above shows the various parts, and in chapter 2, section 40 | **2.7.2 Introducing the architecture of the book selling site application** 41 | I explain the `DataLayer`, `ServiceLayer` and `EfCoreInAction` (ASP.NET Core, presentation layer) projects. 42 | In chapter 4, section 43 | **4.3 A design pattern to help you implement business logic** 44 | I explain the two extra layers, `BizLogic` (pure business logic) and `BizDbAccess` (business database access) projects. 45 | 46 | ### Directory structure of this application 47 | 48 | #### DataLayer 49 | 50 | The directories are: 51 | * `EfClasses` contains all the entity classes used in the database. 52 | * `EfCore` which contains all important 53 | [EfCoreContext.cs](https://github.com/JonPSmith/EfCoreInAction/blob/Chapter02/DataLayer/EfCode/EfCoreContext.cs), 54 | which is the application's DbContext. 55 | * `Migrations` is a directory created by EF Core to handle database migrations (see chapter 2 for a quick overview). 56 | * `QueryObjects` holds the genric paging *query object*, which I describe in chapter 2. 57 | 58 | ### ServiceLayer 59 | 60 | This project goes as the book progresses. It has a series of top-level directories. 61 | The ones to look at end with `...Service`, as they contain the various classes that 62 | are used in the ASP.NET Core presentation layer to query or add, update or delete data. 63 | 64 | These `...Service` directories hold *DTOs* (Data Transfer Objects), which I explain in chapter 2. 65 | They can also contain the following sub-directories: 66 | * `Concrete`, which holds the classes that hold the code that is executed 67 | * `QueryObjects`, which hold *query objects*, which I describe in chapter 2. 68 | 69 | ### EfCoreInAction (ASP.NET Core, presentation layer) 70 | 71 | This follows the normal ASP.NET Core structure. 72 | This book isn't about ASP.NET Core - if you want to learn more about ASP.NET Core then please look at 73 | [ASP.NET Core documentation](https://docs.microsoft.com/en-us/aspnet/core/). 74 | 75 | ## 3. Test - unit test project 76 | 77 | This contains loads of unit tests. Every listing, and many of the facts, in the book are tested in this project. 78 | I use [xUnit](https://xunit.github.io/) as the testing frameword with the fluent asserts. 79 | 80 | The tests are in a directory called `UnitTests`, with sub-directories for each project in the solution. 81 | Each of the test classes starts with the branch they refer to, for instance a test class 82 | whoes name starts with `Ch02_` comes from the branch `Chapter02`. 83 | 84 | This project is fairly complicated and I'm not going explain it, but if you know about iunit tests 85 | then there is a lot of learning to be had looking at the tests. 86 | 87 | 88 | 89 | 90 | 91 | 92 | --------------------------------------------------------------------------------