網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
使用EF?Code?First搭建簡(jiǎn)易ASP.NET?MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移_實(shí)用技巧
作者:Darren?Ji ? 更新時(shí)間: 2022-11-04 編程語(yǔ)言本篇使用EF Code First搭建一個(gè)簡(jiǎn)易ASP.NET MVC 4網(wǎng)站,并允許數(shù)據(jù)庫(kù)遷移。
創(chuàng)建一個(gè)ASP.NET MVC 4 網(wǎng)站。
在Models文件夾內(nèi)創(chuàng)建Person類。
public class Person { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
在Controls文件夾內(nèi)創(chuàng)建PersonController,選擇使用Entity Framework的模版、模型類,創(chuàng)建數(shù)據(jù)上下文類,如下:
點(diǎn)擊"添加"后,除了在Controls文件夾內(nèi)多了PersonController,在Models文件夾中多了PersonContext類,如下:
using System.Data.Entity; namespace MvcApplication1.Models { public class PersonContext : DbContext { // 您可以向此文件中添加自定義代碼。更改不會(huì)被覆蓋。 // // 如果您希望只要更改模型架構(gòu),Entity Framework // 就會(huì)自動(dòng)刪除并重新生成數(shù)據(jù)庫(kù),則將以下 // 代碼添加到 Global.asax 文件中的 Application_Start 方法。 // 注意: 這將在每次更改模型時(shí)銷毀并重新創(chuàng)建數(shù)據(jù)庫(kù)。 // // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcApplication1.Models.PersonContext>()); public PersonContext() : base("name=PersonContext") { } public DbSet<Person> People { get; set; } } }
在Web.config中的connectionStrings多了如下配置,選擇了默認(rèn)的localdb數(shù)據(jù)庫(kù)。
<connectionStrings> ...... <add name="PersonContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=PersonContext-20150210155119; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|PersonContext-20150210155119.mdf" providerName="System.Data.SqlClient" /> </connectionStrings>
在Views/文件夾中多了Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, Index.cshtml這個(gè)幾個(gè)視圖文件。
現(xiàn)在,我們想啟動(dòng)EF的自動(dòng)遷移功能。點(diǎn)擊"工具"-"庫(kù)程序包管理器"-"程序包管理器控制臺(tái)",輸入enable-migrations:
在根目錄下多了一個(gè)Migrations文件夾,以及生成了一個(gè)Configuration類,如下:
namespace MvcApplication1.Migrations { using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication1.Models.PersonContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(MvcApplication1.Models.PersonContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // } } }
以上,我們可以添加一些種子數(shù)據(jù)。
現(xiàn)在需要把種子數(shù)據(jù)遷移到數(shù)據(jù)庫(kù),在"程序包管理器控制臺(tái)",輸入add-migration initial
:
此時(shí),在Migrations文件夾內(nèi)多了201502100756322_initial類,記錄了本次遷移的動(dòng)作。
namespace MvcApplication1.Migrations { using System; using System.Data.Entity.Migrations; public partial class initial : DbMigration { public override void Up() { CreateTable( "dbo.People", c => new { ID = c.Int(nullable: false, identity: true), FirstName = c.String(), LastName = c.String(), }) .PrimaryKey(t => t.ID); } public override void Down() { DropTable("dbo.People"); } } }
最后別忘了要更新數(shù)據(jù)庫(kù),在"程序包管理器控制臺(tái)",輸入update-database:
這時(shí)候,瀏覽/Person/Index,能實(shí)現(xiàn)所有的增刪改功能。
如果這時(shí)候,我們希望在Person中增加一個(gè)屬性,比如類型為int的Age屬性。
public class Person { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
我們?nèi)绾胃嬖V數(shù)據(jù)庫(kù)呢?
還是在"程序包管理器控制臺(tái)",輸入add-migration 名稱
:
此時(shí),在Migrations文件夾內(nèi)多了201502100812315_addedage類,記錄了本次遷移的動(dòng)作。
最后,還在"程序包管理器控制臺(tái)",輸入update-database
以更新數(shù)據(jù)庫(kù)。
為了讓視圖與當(dāng)前Person類同步,可以先后刪除Person文件夾和PersonController控制器,再重新創(chuàng)建PersonController控制器,選擇使用Entity Framework的模版、Person類,PersonContext上下文類。
至此,簡(jiǎn)單體驗(yàn)了EF Code First創(chuàng)建數(shù)據(jù)庫(kù)并實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移的方便之處。
原文鏈接:https://www.cnblogs.com/darrenji/p/4284525.html
相關(guān)推薦
- 2022-05-09 .NET中常見的加解密算法詳解_實(shí)用技巧
- 2023-10-12 ant-design的Input輸入框取消選擇后的藍(lán)色背景以及如何取消提示
- 2022-07-18 實(shí)現(xiàn)?Python?腳本生成命令行_python
- 2022-08-01 flask-SQLALchemy連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例_python
- 2022-07-07 在Kubernetes集群中搭建Istio微服務(wù)網(wǎng)格的過程詳解_云其它
- 2022-05-06 C語(yǔ)言枚舉的使用以及作用_C 語(yǔ)言
- 2023-12-19 Mybatis-plus的快速使用
- 2022-08-20 Python中range()與np.arange()的具體使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支