Building Web Applications with Microsoft ASP

Slides:



Advertisements
Similar presentations
Microsoft SQL Server 2008 From the Program menu choose: Microsoft SQL Server 2008 R2  SQL Server Management Studio. You may see a window indicating the.
Advertisements

Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
Part 05 – Code First Migrations and Azure Deployment Entity Framework and MVC Series Tom Perkins NTPCUG.
ORM Technologies and Entity Framework (EF)
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.
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
OVERVIEW ON HOW ENTITY FRAMEWORK CODE FIRST CAN EASE THE DEVELOPMENT PROCESS Entity Framework Code First 11/19/2013 Joe Walling Copyright Walling Info.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
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.
Standards and Conventions
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
Building Web Applications with Microsoft ASP
ASP.NET Programming with C# and SQL Server First Edition
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)
EF Advanced Querying Optimize Performance SoftUni Team Advanced
EF Relations Object Composition
Entity Framework: Code First
Entity Framework DB From Code, OOP Introduction
Basic Database Concepts
C#: ASP.NET MVC Overview
Data Definition and Data Types
 .NET CORE
Building Web Applications with Microsoft ASP
Entity Framework By: Casey Griffin.
Entity Framework Core for Enterprise Applications
Relational databases, and more …
Learn. Imagine. Build. .NET Conf
ADO.NET Entity Framework
ISC440: Web Programming 2 Server-side Scripting PHP 3
Slides and images stolen from “real” .NET Conf. presenters
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Entity Framework Core.
Repository pattern Andres Käver, IT Kolledž 2016/2017 Spring.
Entity Framework Core (EF Core)
Primary key Introduction Introduction: A primary key, also called a primary keyword, is a key in a relational database that is unique for each record.
Using Visual Studio Visual Studio භාවිතය
Data Management Innovations 2017 High level overview of DB
What’s new in ASP.NET Core and Entity Framework 2.2 (Preview 3)
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
C# - EF Core Intro IT College, Andres Käver, , Fall semester
SQL Server Fundamentals for Beginners
C# - Razor Pages Db/Scaffolding/Async
02 | Beginning Code First Adam Tuliper | Technical Evangelist
Visual Studio 2010 and .NET Framework 4 Training Workshop
SQL AUTO INCREMENT Field
Navigating SSMS Primer for Beginners
Presentation transcript:

Building Web Applications with Microsoft ASP Building Web Applications with Microsoft ASP.NET - I376 Web Application Programming II – I713 IT College, Andres Käver, 2016-2017, Spring semester Web: http://enos.Itcollege.ee/~akaver/ASP.NETCore 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 very much v1 product Some features are still missing (lazy loading) 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 (include prereleases, VS tools) or Microsoft.EntityFrameworkCore.Tools.DotNet

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. Migrations folder is created, with necessary classes for DB creation and Model structure

EF Core – Initial DB Creation In PM console PM> Update-Database PM> Update-Database 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}"); }

EF Core – Include or Exclude You can exclude types or properties from DB Annotations [NotMapped] Fluent API (in dbcontext.OnModelCreating) modelBuilder.Ignore<ModelClass>(); modelBuilder.Entity<ModelCalss>() .Ignore(b => b.ModelProperty);

EF Core – PK Use conventions! <ClassName>Id or just Id Annotations [Key] Fluent API modelBuilder.Entity<ModelClass>() .HasKey(c => c.KeyProperty); Composite key – only in Fluent API modelBuilder.Entity<ModelClass>() .HasKey( c => new { c.KeyProperty1, c.KeyProperty2 });

EF Core – Generated properties Value generation No value Value generated on add Value generated on add or update Annotations [DatabaseGenerated(DatabaseGeneratedOption.None)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [DatabaseGenerated(DatabaseGeneratedOption.Computed)] Fluent API modelBuilder.Entity<ModelClass>().Property( b => KeyProperty.ValueGeneratedNever(); modelBuilder.Entity<ModelClass>().Property( b => b.InsertedProperty).ValueGeneratedOnAdd(); modelBuilder.Entity<ModelClass>().Property( b => b. LastUpdatedProperty).ValueGeneratedOnAddOrUpdate();

EF Core – Required/Optional If property is nullable – by convention not required Annotations [Required] Fluent API modelBuilder.Entity<ModelClass>() .Property( b => b.SomeProperty).IsRequired();

EF Core – Maximum Length By convention depends on the provider Annotation [MaxLength(<Length>)] Fluent API modelBuilder.Entity<ModelClass>().Property( b => b.SomeProperty) .HasMaxLength(<Length>);

EF Core – Indexes Index is created for every foreign key Annotations – not possible Fluent API Single property, non-unique modelBuilder.Entity<ModelClass>() .HasIndex(b => b.SomeProperty); Unique modelBuilder.Entity<ModelClass>() .HasIndex( b => b. SomeProperty) .IsUnique(); More than one property modelBuilder.Entity<ModelClass>() .HasIndex( p => new { p. SomeProperty1, p. SomeProperty2 });

EF Core – Table/Column maping Annotations [Table(“TableName”)] [Column(“ColumnName”)] Fluent API modelBuilder.Entity<ModelClass>() .ToTable(”TableName"); modelBuilder.Entity<ModelClass>() .Property( b => b.SomeProperty) .HasColumnName(”ColumnName");

EF Core – Default value Fluent API modelBuilder.Entity<ModelClass>().Property( b => b.SomeNumber) .HasDefaultValue(3); modelBuilder.Entity<ModelClass>().Property( b => b.SqlProperty) .HasDefaultValueSql("getdate()");