entity framework core - riptutorial.com · 1 1: entity framework 2 2 examples 2 2 sql server entity...

20
Entity Framework Core #entity- framework- core

Upload: others

Post on 26-Aug-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

Entity Framework Core

#entity-

framework-

core

Page 2: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

1

1: Entity Framework 2

2

Examples 2

2

SQL Server Entity Framework 2

1 - .NET Core 2

2 - 3

3 - EF 5

---------------- OR 6

4 - 7

9

10

, 10

10

11

11

12

12

2: EF vs EF6.x 13

13

Examples 13

13

3: 16

16

Examples 16

MVC POST 16

18

Page 3: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: entity-framework-core

It is an unofficial and free Entity Framework Core ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Entity Framework Core.

The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]

https://riptutorial.com/ko/home 1

Page 4: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

1: Entity Framework Entity Framework (EF) Entity Framework .

EF Core .NET .NET O / RM (object-relational mapper). .

Examples

EntityFrameworkCore project.json ( dependencies tools ).

"dependencies": { ... "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0", "Microsoft.EntityFrameworkCore.Design": { "version": "1.0.0", "type": "build" }, }, "tools": { ... "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" }

dotnet restore .

Microsoft SQLServer RDBMS - Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.SqlServer ( Microsoft.EntityFrameworkCore.Sqlite , Npgsql.EntityFrameworkCore.PostgreSQL - RDBMS ).

SQL Server Entity Framework

. . .NET Core Web App Class Project (.NET Core) .

1 - .NET Core

DNX .NET (Hint: You should be able to see the .NET Core option when creating a New Project) -

.NET Core (Visual Studio 2015 Update 3 .) - [ DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1 ] . Visual Studio Check Github .

https://riptutorial.com/ko/home 2

Page 5: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

2 -

ASP.NET ->

https://riptutorial.com/ko/home 3

Page 6: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

Class Library (.NET Core)

https://riptutorial.com/ko/home 4

Page 7: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

3 - EF

project.json :

{ "version": "1.0.0-*", "dependencies": { "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", "NETStandard.Library": "1.6.0" }, "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" }, "frameworks": { "net46": { }, "netcoreapp1.0": { "dependencies": {

https://riptutorial.com/ko/home 5

Page 8: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

"Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-*" } } } } }

References .

---------------- OR

Nuget Package Manager .

Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools –Pre Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

: -

Microsoft.EntityFrameworkCore.Tools

project.json .

"frameworks": { "net46": { }, "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0-*" } }

https://riptutorial.com/ko/home 6

Page 9: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

} }

4 -

Package Manager Console ( )

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer

.

.NET Web App .

project.json project.json ,

dependencies dependencies .

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0", "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

tools :

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

.

project.json .

https://riptutorial.com/ko/home 7

Page 10: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

.

.

https://riptutorial.com/ko/home 8

Page 11: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

.

Package Manager Console .

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

.

https://riptutorial.com/ko/home 9

Page 12: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

Client Ten, Client_2, Client_3 Database Multi Tenant Application . .

OnConfiguring .

public partial class ClientContext { private readonly string _connectionString; public ClientContext(string connectionString) : base() { _connectionString = connectionString; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(_connectionString); } }

.

public void TestConnection() { var clientId = 1; var connectionString = string.Format("Server=192.168.0.211; Database=Client_{0}; user id= USER; password = PWD;", clientId); using (var clientContext = new ClientContext(connectionString)) { var assets = clientContext.Users.Where(s => s.UserId == 1); } }

,

https://riptutorial.com/ko/home 10

Page 13: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

EF . .

, , EF . .

using Microsoft.EntityFrameworkCore; using System.Collections.Generic; namespace Intro { public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } }

LINQ (Language Integrated Query) .

using (var db = new BloggingContext()) { var blogs = db.Blogs .Where(b => b.Rating > 3) .OrderBy(b => b.Url) .ToList(); }

, .

https://riptutorial.com/ko/home 11

Page 14: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

using (var db = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; db.Blogs.Add(blog); db.SaveChanges(); }

LINQ (Language Integrated Query) .

using (var db = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; db.Blogs.Attach(blog); db.Blogs.Remove(blog); db.SaveChanges(); }

.

using (var db = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; var entity = db.Blogs.Find(blog); entity.Url = "http://sample2.com"; db.SaveChanges(); }

Entity Framework : https://riptutorial.com/ko/entity-framework-core/topic/3796/entity-framework--

https://riptutorial.com/ko/home 12

Page 15: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

2: EF vs EF6.x.

Examples

EF Core EF6.x (1) .

.

EF6.x EF 1.0.0

(, )

API

: (TPH)

: (TPT)

: TPC (Table per concrete Class)

:

:

:

:

/

/

:

: EDMX (XML)

:

: VS

https://riptutorial.com/ko/home 13

Page 16: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

EF6.x EF 1.0.0

LINQ :

LINQ :

LINQ :

LINQ :

""SQL

/

: Eager

: Lazy

:

SQL :

SQL :

SQL : LINQ

EF6.x EF 1.0.0

:

:

(N-Tier) : API

(N-Tier) :

EF6.x EF 1.0.0

/ API

(, , ...)

https://riptutorial.com/ko/home 14

Page 17: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

EF6.x EF 1.0.0

SQL

MySQL , (2)

PostgreSQL

, (2)

SQLite

SQL Compact

DB2

InMemory ( )

Azure

EF6.x EF 1.0.0

WinForms

WPF

ASP.NET

ASP.NET

(3)

UWP

:

(1) : 2016/10/18

(2) : , . .

(3) : EF Core Xamarin .NET Standard Xamarin .

EF vs EF6.x : https://riptutorial.com/ko/entity-framework-core/topic/7513/ef--vs-ef6-x

https://riptutorial.com/ko/home 15

Page 18: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

3: EF Core Many to Many :

Examples

MVC POST

Product .

public class Product { public int ProductId { get; set; } public ICollection<ColorProduct> ColorProducts { get; set; } } public class ColorProduct { public int ProductId { get; set; } public int ColorId { get; set; } public virtual Color Color { get; set; } public virtual Product Product { get; set; } } public class Color { public int ColorId { get; set; } public ICollection<ColorProduct> ColorProducts { get; set; } }

.

public static class Extensions { public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class { db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey)); db.Set<T>().AddRange(newItems.Except(currentItems, getKey)); } public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc) { return items .GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems }) .SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp }) .Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T))) .Select(t => t.t.item); } }

https://riptutorial.com/ko/home 16

Page 19: Entity Framework Core - riptutorial.com · 1 1: Entity Framework 2 2 Examples 2 2 SQL Server Entity Framework 2 1 - .NET Core 2 2 - 3 3 - EF 5----- OR 6 4 - 7 9 10, 10

(MVC POST ).

[HttpPost] public IActionResult Edit(ProductVm vm) { if (ModelState.IsValid) { var model = db.Products .Include(x => x.ColorProducts) .FirstOrDefault(x => x.ProductId == vm.Product.ProductId); db.TryUpdateManyToMany(model.ColorProducts, vm.ColorsSelected .Select(x => new ColorProduct { ColorId = x, ProductId = vm.Product.ProductId }), x => x.ColorId); db.SaveChanges(); return RedirectToAction("Index"); } return View(vm); } public class ProductVm { public Product Product { get; set; } public IEnumerable<int> ColorsSelected { get; set; } }

, .

: https://riptutorial.com/ko/entity-framework-core/topic/9527/----

https://riptutorial.com/ko/home 17