C# - EF Core Intro IT College, Andres Käver, , Fall semester

Slides:



Advertisements
Similar presentations
Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
Advertisements

Part 05 – Code First Migrations and Azure Deployment Entity Framework and MVC Series Tom Perkins NTPCUG.
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)
(code name: Data Dude) Josh Robinson Aculix.
CSCI 6962: Server-side Design and Programming
Connecting a.NET application to a database Jim Warren, COMPSCI 280 S Enterprise Software Development.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during WakeUpAndCode.com.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Working with MS SQL Server Textbook Chapter 14.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
DATABASE DEVELOPMENT WITH VSTS DATABASE EDITION By Chris Dahlberg ©2009 Aspect Software, Inc. All rights reserved. 1.
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Data in Windows 10 UWP Andy Wigley XML, JSON, SQLite or EF Core ?
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Windows App Studio Windows App Studio is the tool that makes it fast and easy to build Windows 10 apps. It’s accessible from any device with a browser.
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core * aka ASP.NET 5 before.
Andres Käver, IT Kolledž public interface IPersonRepository : IDisposable { IQueryable All { get; } IQueryable AllIncluding( params Expression.
Building Web Applications with Microsoft ASP
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
Y.-H. Chen International College Ming-Chuan University Fall, 2004
Introduction to .NET Core and EF Core
Introduction to .NET Florin Olariu
Introduction to Entity Framework
EF Code First (Advanced)
TESTING TEST DRIVEN DEVELOPMENT
EF Advanced Querying Optimize Performance SoftUni Team Advanced
Entity Framework DB From Code, OOP Introduction
Basic Database Concepts
C#: ASP.NET MVC Overview
Data Definition and Data Types
Entity Framework By: Casey Griffin.
Entity Framework Core for Enterprise Applications
Data Structures and Database Applications Managing Databases with MVC
Relational databases, and more …
ADO.NET Entity Framework
Buổi 7 Mô hình CSDL Entity Framework code first
Lecturer: Mukhtar Mohamed Ali “Hakaale”
An Introduction to Entity Framework
Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring.
Entity Framework Core (EF Core)
Developing a Model-View-Controller Component for Joomla Part 3
Data Management Innovations 2017 High level overview of DB
Entity Framework Code-First Migrations
IT College 2016, Andres käver
Entity Framework Core for Enterprise Applications
C# - EF Core IT College, Andres Käver, , Fall semester
Building Web Applications with Microsoft ASP
Implementing Entity Framework with MVC Jump Start
SQL Server Fundamentals for Beginners
C# - Razor Pages Db/Scaffolding/Async
Platform Independent Full Stack Development
02 | Beginning Code First Adam Tuliper | Technical Evangelist
Visual Studio 2010 and .NET Framework 4 Training Workshop
Development Environment Setup
SQL AUTO INCREMENT Field
Navigating SSMS Primer for Beginners
Presentation transcript:

C# - EF Core Intro IT College, Andres Käver, 2018-2019, Fall semester Web: http://enos.Itcollege.ee/~akaver/csharp Skype: akaver Email: akaver@itcollege.ee

EF Core Total rewrite of Entity Framework Previous version of EF - EF6 is production ready, fully supported, stable EF Core is production ready! Some features are still missing Some features will never be there Some features will only be in EF Core

EF Core – Console app PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.Tools (VS tools) or Microsoft.EntityFrameworkCore.Tools.DotNet (Command line tools)

EF Core – Model and Context public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Contact> Contacts { get; set; } = new List<Contact>(); } public class ContactType public int ContactTypeId { get; set; } public string ContactTypeValue { get; set; } public class Contact public int ContactId { get; set; } public string ContactValue { get; set; } public Person Person { get; set; } public ContactType ContactType { get; set; }

EF Core – Model and Context public class DataBaseContext : DbContext { public DbSet<Person> People { get; set; } public DbSet<Contact> Contacts { get; set; } public DbSet<ContactType> ContactTypes { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) optionsBuilder.UseSqlServer( @"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"); }

EF Core – Basic stuff Write your models (POCO-s) Every class used in EF needs primary key (PK) By convention, PK-s are named <ClassName>Id or Id Declare context class, derived from DbContext Inside context class add DbSet<YourPoco> If you don’t include all pocos in DbSets, EF will autodiscover them and create tables for them Don’t rely on EF automatic creation of properties and other stuff! Check your DB structure, does it match your model 1:1!

EF Core – Initial DB migration EF Core will not automatically create for you db creation code Open Tools->NuGet Package Manager->Package Manager Console PM> Add-Migration InitialDbCreation To undo this action, use Remove-Migration. Console> dotnet ef migrations add InitialDbCreation Migrations folder is created, with necessary classes for DB creation and Model structure

EF Core – Initial DB Creation In PM console PM> Update-Database Console> dotnet ef database update PM> Update-Database -verbose Executed DbCommand (202ms) [Parameters=[], CommandType='Text', CommandTimeout='60'] CREATE DATABASE [MyDatabase]; Executed DbCommand (58ms) [Parameters=[], CommandType='Text', CommandTimeout='60'] IF SERVERPROPERTY('EngineEdition') <> 5 EXEC(N'ALTER DATABASE [MyDatabase] SET READ_COMMITTED_SNAPSHOT ON;'); Executed DbCommand (12ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [__EFMigrationsHistory] ( [MigrationId] nvarchar(150) NOT NULL, [ProductVersion] nvarchar(32) NOT NULL, CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId]) );

EF Core – Check structure Check DB structure you got! It has to match your model 1:1 Look out for automatically created fields or tables. Use VS View->Server Explorer or MS SQL Server Management Studio Or any other tool suitable for your DB engine EF Core supports several DB engines (most have multiple connector providers)

EF Core – Use your model/context static void Main(string[] args) { using (var db = new DataBaseContext()) db.People.Add(new Person { FirstName="Andres", LastName = "Käver" }); var count = db.SaveChanges(); Console.WriteLine("{0} records saved to database", count); Console.WriteLine(); Console.WriteLine("All people in database:"); foreach (var person in db.People) Console.WriteLine($" - {person.FirstName} {person.LastName}"); }