Book Image

Mastering Entity Framework Core 2.0

By : Prabhakaran Anbazhagan
Book Image

Mastering Entity Framework Core 2.0

By: Prabhakaran Anbazhagan

Overview of this book

Being able to create and maintain data-oriented applications has become crucial in modern programming. This is why Microsoft came up with Entity Framework so architects can optimize storage requirements while also writing efficient and maintainable application code. This book is a comprehensive guide that will show how to utilize the power of the Entity Framework to build efficient .NET Core applications. It not only teaches all the fundamentals of Entity Framework Core but also demonstrates how to use it practically so you can implement it in your software development. The book is divided into three modules. The first module focuses on building entities and relationships. Here you will also learn about different mapping techniques, which will help you choose the one best suited to your application design. Once you have understood the fundamentals of the Entity Framework, you will move on to learn about validation and querying in the second module. It will also teach you how to execute raw SQL queries and extend the Entity Framework to leverage Query Objects using the Query Object Pattern. The final module of the book focuses on performance optimization and managing the security of your application. You will learn to implement failsafe mechanisms using concurrency tokens. The book also explores row-level security and multitenant databases in detail. By the end of the book, you will be proficient in implementing Entity Framework on your .NET Core applications.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Dedication
Preface
4
Building Relationships – Understanding Mapping

Data models


When we think about creating data models in the .NET world way before creating the database, we are a little bit off the legacy track, and yes, it's been widely called the Code-First approach. Let's create entity classes using code-first for the Blogging application, and put them into the Models folder under the project.

Blog entity

Create a Blog.cs class file and include the following properties:

    public class Blog
    {
      public int Id { get; set; }
      public string Url { get; set; }
      public ICollection<Post> Posts { get; set; }    
    }

The Entity Framework will look for any property with the name Id or TypeNameId and marks them as the primary key of the table. The Posts property is a navigation property which contains Post items related to this Blog entity. It doesn't matter whether we use ICollection<T> or IEnumerable<T> for the navigation property, EF will create a collection for us, HashSet<T> by default. We could also create a concrete collection using List<T>.

Post entity

Create a Post.cs class file and include the following properties:

    public class Post
    {
      public int Id { get; set; }
      public string Title { get; set; }
      public string Content { get; set; }
      public DateTime PublishedDateTime { get; set; }
      public int BlogId { get; set; }
      public Blog Blog { get; set; }
    }

The BlogId property is a foreign key created for the corresponding Blog navigation property. As you may notice in this case, we have an individual item as the navigation property, as opposed to a list in the Blog entity. This is where relationship type comes into the picture, which we will be exploring more in Chapter 3, Relationships – Terminology and Conventions.

Note

EF will allow us to create an individual navigation property without any foreign key in the entity. In those cases, EF will create a foreign key for us in the database table using the BlogId pattern (the Blog navigation property along with its  Id primary key). EF will generate them automatically for all navigational properties against the Id primary key, but it also allows us to name it differently and decorate it via a custom attribute.

We have built the schema required for the application so far, but it was not configured in EF, so let's see how the data models get connected/configured with EF using database context.