Introduction to .NET Florin Olariu

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

Entity Framework Code First Migrations
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
ORM Technologies and Entity Framework (EF)
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Entity Framework Code First End to End
Databases and LINQ Visual Basic 2010 How to Program 1.
LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009.
Entity Framework, a quickstart Florin−Tudor Cristea, Microsoft Student Partner.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
Entity Framework 7 Who Are You & What Have You Done to my ORM?
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
1 A Very Brief Introduction to Relational Databases.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core * aka ASP.NET 5 before.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Others Talk, We Listen. Managing Database Projects in Visual Studio 2013.
Introduction to.NET Florin Olariu “Alexandru Ioan Cuza”, University of Iai Department of Computer Science.
Top 10 Entity Framework Features Every Developer Should Know
DevOps with ASP.NET Core and Entity Framework Core
Introduction to Entity framework
ASP.NET Programming with C# and SQL Server First Edition
Building Web Applications with Microsoft ASP
Introduction to .NET Core and EF Core
Visual Basic 2010 How to Program
Introduction to .NET Florin Olariu
Project Management: Messages
Introduction to Entity Framework
Table spaces.
EF Code First (Advanced)
EF Advanced Querying Optimize Performance SoftUni Team Advanced
Working in the Forms Developer Environment
EF Relations Object Composition
Entity Framework: Code First
Best Practices and Architecture
 .NET CORE
Did your feature got in, out or planned?
Entity Framework By: Casey Griffin.
Entity Framework Advanced Querying SoftUni Team Technical Trainers
Contained DB? Did it do something wrong?
Entity Framework Core for Enterprise Applications
Learn. Imagine. Build. .NET Conf
ADO.NET Entity Framework
ADO.NEXT Advances in Data Access for 2008
Microsoft Build /15/2018 6:28 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
An Introduction to Entity Framework
COP5725 DATABASE MANAGEMENT POSTGRESQL TUTORIAL
Entity Framework Core.
Entity Framework Core (EF Core)
What’s new in ASP.NET Core and Entity Framework 2.2 (Preview 3)
Computer Science Projects Database Theory / Prototypes
SSDT and Database Project Basics
Advanced Database Concepts: Reports & Views
IT College 2016, Andres käver
Microsoft SQL Server Upgrade and Downgrade scenarios
Entity Framework Core for Enterprise Applications
C# - EF Core IT College, Andres Käver, , Fall semester
…and web frameworks in general
Introduction to .NET Florin Olariu
Implementing Entity Framework with MVC Jump Start
Entity Framework & LINQ (Language Integrated Query)
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

Introduction to .NET Florin Olariu “Alexandru Ioan Cuza”, University of Iași Department of Computer Science

Entity Framework Core – part 2

Agenda Entity Framework Core – Short recap Entity Framework Core – Features Entity Framework Core – Database providers support Entity Framework Core - Building connection strings Entity Framework Core - Managing migrations Entity Framework Core - Managing navigation properties Entity Framework Core - Using loading patterns in EF Core Entity Framework Core – Demo

Entity Framework Core - Short recap (1/4)

Entity Framework Core - Short recap (2/4) Is a lightweight and extensible version of Entity Framework

Entity Framework Core - Short recap (3/4) Is a lightweight and extensible version of Entity Framework It is based on ORM (Object Relational Mapper) which give us the possibility to work with databases using .NET objects

Entity Framework Core - Short recap (4/4)

Entity Framework Core – Features (1/11) Entity Framework Core supports the following features:

Entity Framework Core – Features (2/11) Modeling Modeling:

Entity Framework Core – Features (3/11) Modeling Fluent API Modeling: 1. Fluent API: This allows us to configure our domain classes. It provides more functionality for configuration as compared to Data Annotation. This allows us to override “OnModelCreating” method of the context class to configure the model.

Entity Framework Core – Features (4/11) Modeling Fluent API Data annotations Modeling: Fluent API: This allows us to configure our domain classes. It provides more functionality for configuration as compared to Data Annotation. This allows us to override “OnModelCreating” method of the context class to configure the model. These are attributes that can be added to the entity classes to configure the model classes.

Entity Framework Core – Features (5/11) Modeling Fluent API Data annotations Shadow properties Modeling: Fluent API: This allows us to configure our domain classes. It provides more functionality for configuration as compared to Data Annotation. This allows us to override “OnModelCreating” method of the context class to configure the model. These are attributes that can be added to the entity classes to configure the model classes. Shadow Properties are properties which are not present in our entity model class. The values can be changed and maintained by the Change Tracker API. They can also participate in LINQ to Entity query, database migration, and Create/Update operations. Shadow properties can be referenced in LINQ queries via the EF.Property static method.

Entity Framework Core – Features (6/11) Modeling Fluent API Data annotations Shadow properties Modeling: Fluent API: This allows us to configure our domain classes. It provides more functionality for configuration as compared to Data Annotation. This allows us to override “OnModelCreating” method of the context class to configure the model. These are attributes that can be added to the entity classes to configure the model classes. Shadow Properties are properties which are not present in our entity model class. The values can be changed and maintained by the Change Tracker API. They can also participate in LINQ to Entity query, database migration, and Create/Update operations. Shadow properties can be referenced in LINQ queries via the EF.Property static method.

Entity Framework Core – Features (7/11) Modeling Fluent API Data annotations Shadow properties It maintains the relation between entities with help of navigation and foreign key properties Modeling: Fluent API: This allows us to configure our domain classes. It provides more functionality for configuration as compared to Data Annotation. This allows us to override “OnModelCreating” method of the context class to configure the model. These are attributes that can be added to the entity classes to configure the model classes. Shadow Properties are properties which are not present in our entity model class. The values can be changed and maintained by the Change Tracker API. They can also participate in LINQ to Entity query, database migration, and Create/Update operations. Shadow properties can be referenced in LINQ queries via the EF.Property static method.

Entity Framework Core – Features (8/11) Modeling Fluent API Data annotations Shadow properties It maintains the relation between entities with help of navigation and foreign key properties Change Tracking Change Tracking: It includes many change tracking methods like Snapshot change tracking and Notification change tracking. We can also access the tracked state of entities via DbContext.Entry and DbContext.ChangeTracker. It has also introduced new API "DbContext.AttachGraph" which helps us re-attach entities to a context, in order to save new/modified entities.

Entity Framework Core – Features (9/11) Modeling Fluent API Data annotations Shadow properties It maintains the relation between entities with help of navigation and foreign key properties Change Tracking SaveChanges SaveChanges This version of the EF supports basic save functionality with async support. It also supports Optimistic Concurrency to protect against overwriting changes made by other users. It provides the API to support "Transaction" feature.

Entity Framework Core – Features (10/11) Modeling Fluent API Data annotations Shadow properties It maintains the relation between entities with help of navigation and foreign key properties Change Tracking SaveChanges Model validation Model validation It helps us in detecting invalid data into model properties and provides the error message to user.

Entity Framework Core – Features (11/11) Modeling Fluent API Data annotations Shadow properties It maintains the relation between entities with help of navigation and foreign key properties Change Tracking SaveChanges Model validation Query Query: It supports LINQ to retrieve the data from the database. It supports the "NoTracking" feature which enables faster query execution because at this time, context is not required to monitor changes made in entities. This version also supports "Eager loading" and "Lazy Loading".

Entity Framework Core – Database providers support(1/7)

Entity Framework Core – Database providers support(2/7) This version of Entity Framework Core 1.0 supports:

Entity Framework Core – Database providers support(3/7) This version of Entity Framework Core 1.0 supports: SQL Server

Entity Framework Core – Database providers support(4/7) This version of Entity Framework Core 1.0 supports: SQL Server SQL Lite

Entity Framework Core – Database providers support(5/7) This version of Entity Framework Core 1.0 supports: SQL Server SQL Lite Postgres

Entity Framework Core – Database providers support(6/7) This version of Entity Framework Core 1.0 supports: SQL Server SQL Lite Postgres SQL Compact

Entity Framework Core – Database providers support(7/7) This version of Entity Framework Core 1.0 supports: SQL Server SQL Lite Postgres SQL Compact Oracle (is work in progress)

Entity Framework Core - Building connection strings(1/5) Most database providers require some form of connection string to connect to the database. Sometimes this connection string contains sensitive information that needs to be protected. You may also need to change the connection string as you move your application between environments, such as development, testing, and production. Depending by application type we have 2 modalities to use a connection string If we are working with Console Application the connection string should be used in OnConfiguring implementation in Context file like in the following example

Entity Framework Core - Building connection strings(2/5) Most database providers require some form of connection string to connect to the database. Sometimes this connection string contains sensitive information that needs to be protected. You may also need to change the connection string as you move your application between environments, such as development, testing, and production. Depending by application type we have 2 modalities to use a connection string If we are working with Console Application the connection string should be used in OnConfiguring implementation in Context file like in the following example If we are using the Asp.Net Core application this connection string should be stored in appsettings.json as an environment variable like in the following sample

Entity Framework Core - Building connection strings(3/5) Most database providers require some form of connection string to connect to the database. Sometimes this connection string contains sensitive information that needs to be protected. You may also need to change the connection string as you move your application between environments, such as development, testing, and production. Depending by application type we have 2 modalities to use a connection string If we are working with Console Application the connection string should be used in OnConfiguring implementation in Context file like in the following example If we are using the Asp.Net Core application this connection string should be stored in appsettings.json as an environment variable like in the following sample OBS: It will be configured later into Startup.cs file

Entity Framework Core - Building connection strings(4/5) For Postgre SQL : Nuget Package: Npqsql.EntityFrameCore.PostgreSQL(code first) Nuget Package: Npqsql.EntityFrameCore.PostgreSQL.Design(database first)

Entity Framework Core - Building connection strings(5/5) For SQL local DB Nuget Package: Microsoft.EntityFrameworkCore.SqlServer (code first) Nuget Package: Microsoft.EntityFrameworkCore.SqlServer.Design (database first)

Entity Framework Core - Managing migrations (1/8) The Migrations feature enables you to change the data model and deploy your changes to production by updating the database schema without having to drop and re-create the database.

Entity Framework Core - Managing migrations (2/8) Enable-Migrations (2) In EF Core we will receive this message if we will try to enable migrations:

Entity Framework Core - Managing migrations (3/8) Enable-Migrations Generating & Running Migrations (3)It is used to store any changes within the entity. For the previous versions we should use this. Creates Migrations folder within the project.

Entity Framework Core - Managing migrations (4/8) Enable-Migrations Generating & Running Migrations Add-Migration Code First Migrations has two primary commands that you are going to become familiar with. (4)Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created

Entity Framework Core - Managing migrations (5/8) Enable-Migrations Generating & Running Migrations Add-Migration Update-Database Code First Migrations has two primary commands that you are going to become familiar with. Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created. (5)Update-Database will apply any pending migrations to the database.

Entity Framework Core - Managing migrations (6/8) Enable-Migrations Generating & Running Migrations Code First Migrations has two primary commands that you are going to become familiar with. Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created. Update-Database will apply any pending migrations to the database. (6)If we will change the structure of an entity(by adding a new property for instance) without creating migrations we will end up with the following error message:

Entity Framework Core - Managing migrations (7/8) Enable-Migrations Generating & Running Migrations Code First Migrations has two primary commands that you are going to become familiar with. Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created. Update-Database will apply any pending migrations to the database. (6)If we will change the structure of an entity(by adding a new property for instance) without creating migrations we will end up with the following error message: (7)The migration file structure is presented in the following image. -> Up=>Used for Upgrade, Down=>Used for downgrade.

Entity Framework Core - Managing migrations (8/8) Enable-Migrations Generating & Running Migrations Code First Migrations has two primary commands that you are going to become familiar with. Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created. Update-Database will apply any pending migrations to the database. (6)If we will change the structure of an entity(by adding a new property for instance) without creating migrations we will end up with the following error message: (7)The migration file structure is presented in the following image. (8) We can execute directly SQL Commands like the following into Up/Down methods:

Entity Framework Core - Managing navigation properties(1/14) Entity Framework supports 3 types of relations (using navigation properties):

Entity Framework Core - Managing navigation properties(2/14) One to One => 1:1 Entity Framework supports 3 types of relations (using navigation properties): One to one

Entity Framework Core - Managing navigation properties(3/14) One to One => 1:1 One to Many => 1: many Entity Framework supports 3 types of relations (using navigation properties): One to one One to many

Entity Framework Core - Managing navigation properties(4/14) One to One => 1:1 One to Many => 1: many Many to Many => many : many Entity Framework supports 3 types of relations (using navigation properties): One to one One to many Many to many Note: These 3 types are exactly like in relational database.

Entity Framework Core - Managing navigation properties(5/14) One to One => 1:1 One to Many => 1: many Many to Many => many : many Entity Framework supports 3 types of relations (using navigation properties): One to one One to many Many to many Note: These 3 types are exactly like in relational database.

Entity Framework Core - Managing navigation properties(6/14) One to One relationship

Entity Framework Core - Managing navigation properties(7/14) One to One relationship

Entity Framework Core - Managing navigation properties(8/14) One to One relationship As you can see in the above figure, Student and StudentAddress have a One-to-One relationship (zero or one). A student can have only one or zero address. Entity framework adds Student navigation property into StudentAddress entity and StudentAddress navigation entity into Student entity. Also, StudentAddress entity has StudentId property as PrimaryKey which makes it a One-to-One relationship.

Entity Framework Core - Managing navigation properties(9/14) One-to-Many relationship

Entity Framework Core - Managing navigation properties(10/14) One-to-Many relationship

Entity Framework Core - Managing navigation properties(11/14) One-to-Many relationship The Standard and Teacher entities have a One-to-Many relationship marked by multiplicity where 1 is for One and * is for many. This means that Standard can have many Teachers whereas Teacher can associate with only one Standard. To represent this, The Standard entity has the collection navigation property Teachers (please notice that it's plural), which indicates that one Standard can have a collection of Teachers (many teachers). And Teacher entity has a Standard navigation property (Not a Collection) which indicates that Teacher is associated with one Standard. Also, it contains StandardId foreign key (StandardId is a PK in Standard entity). This makes it One-to-Many relationship.

Entity Framework Core - Managing navigation properties(12/14) Many-to-Many relationship

Entity Framework Core - Managing navigation properties(13/14) Many-to-Many relationship

Entity Framework Core - Managing navigation properties(14/14) Many-to-Many relationship Student and Course have Many-to-Many relationships marked by * multiplicity. It means one Student can enrol for many Courses and also, one Course can be be taught to many Students. The database design includes StudentCourse joining table which includes the primary key of both the tables (Student and Course table). Entity Framework represents many-to-many relationships by not having entityset for the joining table in CSDL, instead it manages this through mapping. As you can see in the above figure, Student entity includes Courses property and Course entity includes Students property to represent many-to-many relationship between them.

Entity Framework Core - Using loading patterns in EF Core(1/19) Entity Framework Core allows you to use the navigation properties in your model to load related entities. There are three common O/RM patterns used to load related data.

Entity Framework Core - Using loading patterns in EF Core(2/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed. 

Entity Framework Core - Using loading patterns in EF Core(3/19) Lazy loading Eager loading Eager loading : means that the related data is loaded from the database as part of the initial query.

Entity Framework Core - Using loading patterns in EF Core(4/19) Lazy loading Eager loading Explicit loading Explicit loading: means that the related data is explicitly loaded from the database at a later time.

Entity Framework Core - Using loading patterns in EF Core(5/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed. 

Entity Framework Core - Using loading patterns in EF Core(6/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed.  Once the query is executed the following result might be showed:

Entity Framework Core - Using loading patterns in EF Core(7/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed.  Once the query is executed the following result might be showed:

Entity Framework Core - Using loading patterns in EF Core(8/19) Lazy loading Let’s change the message from loop in order to show the products. Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed.  Once the query is executed the following result might be showed: The message from foreach loop:

Entity Framework Core - Using loading patterns in EF Core(9/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed.  Once the query is executed the following result might be showed: The message from foreach loop: The result will be:

Entity Framework Core - Using loading patterns in EF Core(10/19) Lazy loading Lazy loading: Means that the related data is transparently loaded from the database when the navigation property is accessed.  Once the query is executed the following result might be showed: The message from foreach loop: The result will be: Conlusion:You will notice that when the code accesses the Products property, EF automatically checks to see whether they are loaded. If not, EF loads them for us "lazily.“ Cons: The problem with lazy loading is that multiple round trips to the database server are required to eventually fetch all the data.

Entity Framework Core - Using loading patterns in EF Core(11/19) Eager loading Eager loading: means that the related data is loaded from the database as part of the initial query. Sometimes, it is better to disable lazy loading and manually specify that all the data is brought across the network immediately using eager loading (aka early loading).

Entity Framework Core - Using loading patterns in EF Core(12/19) Eager loading Eager loading: means that the related data is loaded from the database as part of the initial query. Sometimes, it is better to disable lazy loading and manually specify that all the data is brought across the network immediately using eager loading (also known as early loading). In this case the result for the latest query will be:

Entity Framework Core - Using loading patterns in EF Core(13/19) Eager loading Eager loading: means that the related data is loaded from the database as part of the initial query. Sometimes, it is better to disable lazy loading and manually specify that all the data is brought across the network immediately using eager loading (also known as early loading). In this case the result for the latest query will be: In order to solve the problem we have to use “Include” as extension method.

Entity Framework Core - Using loading patterns in EF Core(14/19) Eager loading

Entity Framework Core - Using loading patterns in EF Core(15/19) Explicit loading Explicit loading: means that the related data is explicitly loaded from the database at a later time. It works similar to lazy loading, but you are in control of exactly which related data is loaded and when.

Entity Framework Core - Using loading patterns in EF Core(16/19) Explicit loading Explicit loading: means that the related data is explicitly loaded from the database at a later time. It works similar to lazy loading, but you are in control of exactly which related data is loaded and when.

Entity Framework Core - Using loading patterns in EF Core(17/19) Tips Explicit loading: means that the related data is explicitly loaded from the database at a later time. It works similar to lazy loading, but you are in control of exactly which related data is loaded and when.

Entity Framework Core - Using loading patterns in EF Core(18/19) Tips Carefully consider which loading pattern is best for your code. Explicit loading: means that the related data is explicitly loaded from the database at a later time. It works similar to lazy loading, but you are in control of exactly which related data is loaded and when.

Entity Framework Core - Using loading patterns in EF Core(19/19) Tips Carefully consider which loading pattern is best for your code. The default of lazy loading can literally make you into a lazy database developer! Explicit loading: means that the related data is explicitly loaded from the database at a later time. It works similar to lazy loading, but you are in control of exactly which related data is loaded and when.

Entity Framework Core – Demo(1/2)

Entity Framework Core – Demo(2/2) Targets: How the model should be created How the navigation properties should be managed Manipulating connection string Database result Manipulating data using EF loading patterns

One more thing…(1/2) “You’ve probably noticed that I write a lot about Entity Framework, the Microsoft Object Relational Mapper (ORM) that’s been the prime .NET data access API since 2008. There are other .NET ORMs out there but a particular category, micro-ORMs, gets a lot of notice for great performance. The micro-ORM I’ve heard mentioned most is Dapper. What finally piqued my interest enough to take some time out to crack it open recently was various developers reporting that they’ve created hybrid solutions with EF and Dapper, letting each ORM do what it’s best at within a single application.” Julie Lerman – May 2016

One more thing…(2/2)

Bibliography Price, Mark. C# 6 and .NET Core 1.0: Modern Cross-Platform Development (Kindle). Packt Publishing. Kindle Edition. https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx http://www.c-sharpcorner.com/article/introduction-to-entity-framework- core/ https://docs.efproject.net/en/latest/platforms/full-dotnet/new-db.html

Questions Do you have any other questions?

Thanks! See you next time! 