へっぽこプログラマーの備忘録
プログラムを中心とした個人的なメモ用のブログです。 タイトルは迷走中。
内容の保証はできませんのであしからずご了承ください。
menu
keyboard_arrow_up
Top
search
close
home
ホーム
computer
PC一般
construction
開発環境・ツール
code
プログラミング
home
ホーム
computer
PC一般
construction
開発環境・ツール
code
プログラミング
Home
›
Entity Framework Core
›
Entity Framework Core におけるデータの保存
2018/01/30
Entity Framework Core におけるデータの保存
update
event_note
label
C#
label
Entity Framework Core
Entity Framework Core におけるデータの基本的な保存方法と、エンティティ同士が関連している場合のデータの保存方法についてです。
公式サイトに詳しい解説があるので、ほとんど訳しただけになってしまいました・・・。 - https://docs.microsoft.com/en-us/ef/core/saving/basic - https://docs.microsoft.com/en-us/ef/core/saving/related-data 訳が微妙なので、原文を読まれたほうが良いかもしれません。 ## 環境 - Visual Studio 2017 - .NET Core 2.0 - Entity Framework Core 2.0 ## 扱うモデル 公式サイトと同じ、以下のモデルを基本的な例とします。 (場合によっては別の例が挙げられていますが・・・) ```cs // Blog is the principal entity public class Blog { // Blog.BlogId is the principal key // (in this case it is a primary key rather than an alternate key) public int BlogId { get; set; } public string Url { get; set; } // Blog.Posts is a collection navigation property public List
Posts { get; set; } } // Post is the dependent entity public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } // Post.BlogId is the foreign key public int BlogId { get; set; } // Post.Blog is a reference navigation property // Post.Blog is the inverse navigation property of Blog.Posts (and vice versa) public Blog Blog { get; set; } } ``` ## 基本操作 ### データの追加 `DbSet.Add` メソッドを使用して、エンティティクラスの新しいインスタンスを追加します。 `SaveChanges` を呼び出すと、データがデータベースに挿入されます。 ```cs using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; context.Blogs.Add(blog); context.SaveChanges(); Console.WriteLine(blog.BlogId + ": " + blog.Url); } ``` ### データの更新 EF は、コンテキストによって追跡される既存のエンティティに対する変更を自動的に検出します。 これには、データベースから取得または照会するエンティティ、および以前にデータベースに追加および保存されたエンティティが含まれます。 従って、プロパティに割り当てられた値を変更したら、`SaveChanges` を呼び出すだけでデータを更新できます。 ```cs using (var context = new BloggingContext()) { var blog = context.Blogs.First(); blog.Url = "http://sample.com/blog"; context.SaveChanges(); } ``` ここで、以下のようにインスタンスを丸ごと差し替えてしまうとデータの更新は反映されないので、やってはいけません。 ```cs using (var context = new BloggingContext()) { var blog = context.Blogs.First(); // これだとデータは更新されない blog.Url = new Blog { Url = "http://sample.com" }; context.SaveChanges(); } ``` 必ずプロパティ毎にデータをセットする必要があります。 ### データの削除 エンティティクラスのインスタンスを削除するには、`DbSet.Remove` メソッドを使用します。 エンティティがデータベースに存在する場合、`SaveChanges` の実行中に削除されます。 エンティティがまだデータベースに保存されていない(追加されたとして追跡されている)場合、そのエンティティはコンテキストから削除され、`SaveChanges` が呼び出されると挿入されなくなります。 ```cs using (var context = new BloggingContext()) { var blog = context.Blogs.First(); context.Blogs.Remove(blog); context.SaveChanges(); } ``` ### 1つの SaveChanges での複数の操作を行う 複数の `Add` `Update` `Remove` の操作を1つの `SaveChanges` に対して組み合わせることもできます。 尚、ほとんどのデータベースプロバイダーにおいて、`SaveChanges` はトランザクショナルです。 これは、すべての操作が成功または失敗のどちらかであり、操作が部分的に適用されることはないということを意味します。 ```cs using (var context = new BloggingContext()) { // seeding database context.Blogs.Add(new Blog { Url = "http://sample.com/blog" }); context.Blogs.Add(new Blog { Url = "http://sample.com/another_blog" }); context.SaveChanges(); } using (var context = new BloggingContext()) { // add context.Blogs.Add(new Blog { Url = "http://sample.com/blog_one" }); context.Blogs.Add(new Blog { Url = "http://sample.com/blog_two" }); // update var firstBlog = context.Blogs.First(); firstBlog.Url = ""; // remove var lastBlog = context.Blogs.Last(); context.Blogs.Remove(lastBlog); context.SaveChanges(); } ``` ## 関連データの保存 リレーションシップについては以下の記事に書いているので、そちらを参照してください。 - [Entity Framework Core におけるリレーションシップについて](http://kuttsun.blogspot.jp/2018/01/entity-framework-core_11.html) ### 新規作成 英語では **Adding a graph of new entities** と書かれていました。 - https://docs.microsoft.com/en-us/ef/core/saving/related-data 親のテーブルに新規にレコードを作成し、その子となるテーブルにもレコードを作成することを、エンティティのグラフを追加すると表現しているようですね。 以下がコードの例です。 ```cs using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://blogs.msdn.com/dotnet", Posts = new List
{ new Post { Title = "Intro to C#" }, new Post { Title = "Intro to VB.NET" }, new Post { Title = "Intro to F#" } } }; context.Blogs.Add(blog); context.SaveChanges(); } ``` この場合、`Blogs` のテーブルと `Posts` のテーブルにそれぞれデータが挿入されます。 `Blog` クラスには `Posts` という Navigation property があるため、`Blogs` テーブルと `Posts` テーブルのリレーションシップは EF Core によって自動的に解決されます。 ### 関連するエンティティの追加 Navigation property に新規にエンティティを追加すると、関連するテーブルに自動的にレコードが追加されます。 以下がコード例です。 ```cs using (var context = new BloggingContext()) { var blog = context.Blogs.Include(b => b.Posts).First(); var post = new Post { Title = "Intro to EF Core" }; blog.Posts.Add(post); context.SaveChanges(); } ``` データベースからフェッチされた `Blog` エンティティの `Posts` プロパティに新しくデータを追加しています。 この結果、データベースの `Posts` テーブルにもレコードが追加されます。 ### リレーションシップの変更 エンティティの Navigation property を変更すると、対応する変更がデータベースの外部キー列に対して行われます。 次の例では、`post` エンティティは新しい `blog` エンティティに属するように更新されます。 Navigation property である `post.Blog` に新しい `blog` エンティティをセットしているからです。 また、`blog` はデータベースにも挿入されることに注意してください。 `blog` はすでにコンテキスト(`post`)によって追跡されているエンティティの Navigation property から参照されている新しいエンティティであるためです。 ```cs using (var context = new BloggingContext()) { var blog = new Blog { Url = "http://blogs.msdn.com/visualstudio" }; var post = context.Posts.First(); post.Blog = blog; context.SaveChanges(); } ``` ### リレーションシップの削除 リレーションシップを削除するには、Navigation propery を `null` に設定するか、関連するエンティティを Navigation propery のコレクションからから削除します。 リレーションシップを削除すると、そのリレーションシップで設定されたカスケード削除動作に従って、エンティティの依存関係に副作用が生じる可能性があります。 デフォルトでは、必要な関係に従ってカスケード削除動作が設定され、子エンティティと依存エンティティがデータベースから削除されます。 そのリレーションシップがオプションの関係の場合、カスケード削除はデフォルトでは設定されていませんが、外部キーのプロパティは `null` に設定されます。 リレーションシップの必要性の設定方法については、[Required and Optional Relationships](https://docs.microsoft.com/en-us/ef/core/modeling/relationships#required-and-optional-relationships) を参照してください。 カスケード削除動作の仕組み、それを明示的に設定する方法、EF の規則によってどのように選択されるかの詳細については、[Cascade Delete](https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete) を参照してください。 次の例では、カスケード削除が `Blog` と `Post` の関係に対して設定されているので、`post` エンティティはデータベースから削除されます。 ```cs using (var context = new BloggingContext()) { var blog = context.Blogs.Include(b => b.Posts).First(); var post = blog.Posts.First(); blog.Posts.Remove(post); context.SaveChanges(); } ```
tweet
facebook
Pocket
B!
はてブ
LINE
chevron_left
chevron_right
Translate
Popular Posts
Docker for Windows の設定
TortoiseGit でコミットメッセージを変更する
image
NO IMAGE
smbclient で session setup failed: NT_STATUS_LOGON_FAILURE が表示される
TortoiseGit でブランチ間の差分を見る
image
NO IMAGE
マージ元ブランチとマージ先ブランチ
[Python] 文字列の判定で、None と空文字を同時に判定する
[Python] Tesseract で OCR を行ったら `UnicodeEncodeError: 'ascii' codec can't encode characters` のエラーが表示された
image
NO IMAGE
GitLab Runner でクローンする URL を変更する
[Python] matplotlib で plot する際に "Tcl_AsyncDelete: async handler deleted by the wrong thread" というエラーがでる
image
NO IMAGE
[JavaScript] コンストラクタで await したい
Labels
.NET Core
31
.NET Framework
17
.NET Standard
2
AdminLTE
1
AI
1
Apache
3
AppVeyor
2
AsciiDoc
3
ASP.NET Core
55
Atom
4
AWS
5
AWS Cloud9
4
blockdiag
1
Blogger
13
Bootstrap
3
C/C++
6
C#
106
CentOS
3
Chrome
1
Chronograf
3
chrony
1
Codecov
1
CSS
1
Docker
79
DokuWiki
4
Doxygen
1
draw.io
1
EasyTag
1
Electron
1
Electron.NET
2
Entity Framework Core
9
Excel
2
FFmpeg
3
Firefox
6
Flask
1
Git
19
GitBook
4
GitBucket
7
GitHub
7
GitLab
39
Go
1
Google
1
Google Cloud Platform
1
Grafana
13
GStreamer
2
HTML
5
IIS
8
InfluxDB
14
JavaScript
15
Jenkins
7
Linux
33
Log4View
1
MahApps.Metro
3
MaterialDesignInXamlToolkit
1
MkDocs
2
MongoDB
5
MVC
1
MVVM
6
nginx
3
NLog
3
Node.js
8
npm
1
NVIDIA
3
onvif
1
OpenAPI
2
OpenCV
4
OpenSSL
3
OpenVINO
2
ownCloud
2
pandas
1
Pine Script
1
PlantUML
5
Portainer
3
PowerShell
8
Prism
2
PySide
17
Python
85
PyTorch
1
RabbitVCS
1
Razor
3
Redmine
33
Redoc
1
remark.js
2
rocketchat
10
Ruby
3
scikit-learn
1
SignalR
1
Slack
1
Socket.IO
1
SonarQube
5
Sphinx
10
SQL Server
5
SQLite
1
StableDiffusion
1
Subversion
2
Swagger
1
Swarmpit
1
Syslog
3
Telegraf
6
Tesseract
3
TestLink
2
Tomcat
2
TortoiseGit
11
TortoiseSVN
2
Trading View
1
Traefik
3
Travis CI
1
Ubuntu
31
Visual Studio
39
Visual Studio Code
10
VSCode
8
Vue.js
8
Windows
62
Windows 10
5
Windows ADK
1
Windows API
2
Windows Embedded
4
wkhtmltopdf
2
Word
3
WPF
12
WSL
5
WSL2
5
Xamarin
1
xUnit
5
yaml
1
yolo
1
アプリケーション
1
デザインパターン
1
テスト
1
バッチファイル
2
プログラミング
4
ライセンス
1
暗号資産(仮想通貨)
1
英語
2
確定申告
1
機械学習
1
強化学習
1
雑記
1
書籍
1
数学
1
正規表現
1
Blog Archive
►
2024
(7)
►
3月
(1)
►
2月
(5)
►
1月
(1)
►
2023
(30)
►
12月
(3)
►
11月
(5)
►
10月
(2)
►
9月
(1)
►
8月
(2)
►
7月
(4)
►
6月
(2)
►
5月
(3)
►
4月
(2)
►
3月
(2)
►
2月
(3)
►
1月
(1)
►
2022
(106)
►
12月
(5)
►
11月
(1)
►
10月
(3)
►
9月
(6)
►
8月
(7)
►
7月
(6)
►
6月
(13)
►
5月
(9)
►
4月
(15)
►
3月
(11)
►
2月
(14)
►
1月
(16)
►
2021
(85)
►
12月
(11)
►
11月
(6)
►
10月
(4)
►
9月
(10)
►
8月
(8)
►
7月
(4)
►
6月
(18)
►
5月
(7)
►
4月
(8)
►
3月
(2)
►
2月
(2)
►
1月
(5)
►
2020
(56)
►
12月
(1)
►
11月
(3)
►
10月
(3)
►
9月
(3)
►
8月
(3)
►
7月
(7)
►
6月
(7)
►
5月
(2)
►
4月
(6)
►
3月
(6)
►
2月
(3)
►
1月
(12)
►
2019
(92)
►
12月
(13)
►
11月
(9)
►
10月
(3)
►
9月
(2)
►
8月
(3)
►
7月
(5)
►
6月
(11)
►
5月
(6)
►
4月
(17)
►
3月
(9)
►
2月
(6)
►
1月
(8)
▼
2018
(100)
►
12月
(1)
►
11月
(11)
►
10月
(8)
►
9月
(6)
►
8月
(10)
►
7月
(10)
►
6月
(8)
►
5月
(9)
►
4月
(8)
►
3月
(14)
►
2月
(4)
▼
1月
(11)
Entity Framework Core におけるデータの保存
Moq のインストール
ASP.NET Core で SignalR を使用する
Firefox で英単語を簡単に調べられるアドオン
画像を svg から emf に変換する
Entity Framework Core におけるデータの取得
draw.io で作成した SVG で not supported by viewer と表示される。
dotnet ef コマンドを有効にする
Entity Framework Core におけるリレーションシップについて
Entity Framework Core の基本的な使い方
コマンド "dotnet-ef" に一致する実行可能ファイルが見つかりません
►
2017
(117)
►
12月
(14)
►
11月
(20)
►
10月
(17)
►
9月
(19)
►
8月
(10)
►
7月
(8)
►
6月
(3)
►
5月
(6)
►
4月
(5)
►
3月
(2)
►
2月
(8)
►
1月
(5)
►
2016
(91)
►
12月
(5)
►
11月
(9)
►
10月
(11)
►
9月
(9)
►
8月
(6)
►
7月
(14)
►
6月
(14)
►
5月
(11)
►
4月
(10)
►
3月
(2)
►
2015
(23)
►
12月
(4)
►
11月
(2)
►
10月
(8)
►
9月
(8)
►
7月
(1)
►
2013
(3)
►
11月
(1)
►
9月
(1)
►
7月
(1)
►
2012
(2)
►
7月
(1)
►
6月
(1)
►
2011
(1)
►
9月
(1)
►
2009
(1)
►
7月
(1)
►
2008
(2)
►
11月
(1)
►
7月
(1)
►
2007
(3)
►
10月
(3)