<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aek Thong-aom on Medium]]></title>
        <description><![CDATA[Stories by Aek Thong-aom on Medium]]></description>
        <link>https://medium.com/@aeksaekhow?source=rss-e0853315841f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*k7WjymmSMYQ_bR1SY4Tskg.jpeg</url>
            <title>Stories by Aek Thong-aom on Medium</title>
            <link>https://medium.com/@aeksaekhow?source=rss-e0853315841f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:21:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aeksaekhow/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[การใช้งาน Database Migration ของ Entity Framework Core]]></title>
            <link>https://medium.com/@aeksaekhow/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-database-migration-%E0%B8%82%E0%B8%AD%E0%B8%87-entity-framework-core-c473dd808ac3?source=rss-e0853315841f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c473dd808ac3</guid>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Aek Thong-aom]]></dc:creator>
            <pubDate>Fri, 12 Apr 2019 10:26:11 GMT</pubDate>
            <atom:updated>2020-05-31T09:17:10.693Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*auwEbNcWEXss4qRpo5pJ7g.png" /></figure><p>สวัสดีครับ ผมจะมาสอนวิธีการทำ Database Migration ของ EF Core</p><p><em>ก่อนอื่นเลย ทำไมต้องทำ Database Migration?</em></p><p>เพราะฐานข้อมูลที่เราสร้างไปแล้วมีโอกาสเปลี่ยนแปลงในอนาคต เราจำเป็นต้องเขียน SQL Script เพื่อเปลี่ยนแปลง Database Schema ให้สอดคล้องกับสิ่งที่เราต้องการ การทำ Database Migration มีหลากหลายวิธี ซึ่ง EF Core ก็มีขั้นตอนการทำของมันที่เราแทบไม่ต้องไปยุ่งกับ SQL Script เพียงแค่จัดการกับคลาส Entity กับ DbContext เท่านั้น</p><p>สมมุติเราสร้างโปรเจ็คที่มีข้อมูล Users</p><p>ภายในโปรเจ็ค Repository จะมีคลาส UserEntity</p><pre>public class UserEntity<br>{<br>    public long Id { get; set; }<br>    public string Name { get; set; }<br>}</pre><p>ให้เพิ่ม UserEntity เข้าไปใน DbContext</p><pre>public class MyDbContext : DbContext<br>{<br>    protected override void OnModelCreating(ModelBuilder modelBuilder)<br>    {<br>        modelBuilder.Entity&lt;UserEntity&gt;().ToTable(&quot;Users&quot;);<br>    }<br> <br>}</pre><p>ตรวจสอบ Connection String ให้ถูกต้อง เซ็ต Startup Project ให้ตัวโปรเจ็คหลัก เปิดหน้าต่าง Package Manager Console แล้วเลือก Default project ไปที่โปรเจ็ค Repository จากนั้นพิมคำสั่ง</p><pre>add-migration &lt;migration-name&gt;</pre><p>EF Core จะสร้าง Migration Script ภายใต้โฟลเดอร์ Migrations ในโปรเจ็ค Repository พร้อมกับไฟล์ MyDbContextModelSnapshot.cs</p><p>ภายในไฟล์ MyDbContextModelSnapshot.cs จะบันทึกโครงสร้างคลาส Entity ต่างๆไว้ ครั้งต่อไปที่เราแก้ไขคลาส Entity ระบบมันจะหาการเปลี่ยนแปลงแล้วสร้าง Migration Script ให้คล้องกับการเพิ่มหรือลด Column หรือ Table ต่างๆ</p><p>จบท้ายด้วยการรันคำสั่งด้านล่าง เพื่ออัพเดท Database Schema</p><pre>update-database</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c473dd808ac3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>