???CodeFirst???????????????
????????? ???????[ 2016/7/14 10:22:20 ] ????????????? CodeFirst
????2. ?????????Code Fist Migration)
??????????????Author????????????Email?????????£?
????[Table("T_Authors")]
????public class Author
????{
????public int Id { set; get; }
????public string Name { set; get; }
????public string Email { set; get; }
????public virtual ICollection<Blog> Blogs { set; get; }
????}
???????????????и???ó??????????
????An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
????The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to
????update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
????????????????????????????model??????仯??????????????????????и?????????????2??????г???
????2.1 ???????
????????????????????????????巽??????Packge Manager Console??????Enable-Migrations?????ɡ?
????????????????????????????????е???????
?????????????????????????????????????????????????????????????????????????????Migrations????в???Packge Manager Console??????
????Enable-Migrations –EnableAutomaticMigrations?????????Migrations????е?Configuration.cs?????????AutomaticMigrationsEnabled?????true???ɡ?
????Enable-Migrations???????????????????????????????????
????Configuration??? ????????????????????????????????????
????InitialCreate??? ????????????? Code First ???????????????????????????е????????????????????????????????????????????????????????а??????????δ??????????????? InitialCreate ???????????С????????ε??? Add-Migration ????????????Щ??????????????????
????2.2 ?????????
????????????????Packge Manager Console?м???????Update-Database????????????????????????????
????????????????????????????????????????????????????????????Add-Migration??????????????????
????2.2.1 ???????????
??????????Packge Manager Console??????????Add-Migration AddEmail?????????????????????Migrations????????????????_AddEmail???????????????£?
1 public partial class AddEmail : DbMigration
2 {
3 public override void Up()
4 {
5 //???????????????????dbo.T_Blogs??dbo.T_Authors???????????????
6 //T_Blogs??T_Authors?????????????????????T_Blogs??T_Authors
7 //???????dbo???ò????SQL SERVER?е??????????????С????????MYSQL
8 CreateTable(
9 "dbo.T_Blogs"??
10 c => new
11 {
12 Id = c.Int(nullable: false?? identity: true)??
13 Author_Id = c.Int()??
14 })
15 .PrimaryKey(t => t.Id)
16 .ForeignKey("dbo.T_Authors"?? t => t.Author_Id)
17 .Index(t => t.Author_Id);
18
19 AddColumn("dbo.T_Authors"?? "Email"?? c => c.String(unicode: false));
20 }
21
22 public override void Down()
23 {
24 DropForeignKey("dbo.T_Blogs"?? "Author_Id"?? "dbo.T_Authors");
25 DropIndex("dbo.T_Blogs"?? new[] { "Author_Id" });
26 DropColumn("dbo.T_Authors"?? "Email");
27 DropTable("dbo.T_Blogs");
28 }
29 }
30
??????????????Up?????е?????AddColumn??????????????????Email??Ρ??????????Packge Manager Console??????????Update-Database????????????Update-Database -Verbose?????????????????????SQL??????й???????-Verbose??-Database????и?????????У????????????????У??????????в??????T_Authors????????????????Email?????????__migrationhistory?ж?????????θ????????????
?????????????????????????м???
public partial class AddEmail : DbMigration
{
public override void Up()
{
AddColumn("T_Authors"?? "Email"?? c => c.String(unicode: false));
}
public override void Down()
{
DropColumn("T_Authors"?? "Email");
}
}
??????????????????????????????????????????????Model???磺
???????????Add-Migration addAge??????????μ????????????????£?
public partial class AddAge : DbMigration
{
public override void Up()
{
AddColumn("T_Authors"?? "Age"?? c => c.Int(nullable: false??defaultValue:18));
}
public override void Down()
{
DropColumn("T_Authors"?? "Age");
}
}
???????????????Update-Database??????????????T_Authors???д????????Age?????????????????????????3????е?Model???????????
????2.2.2 ??????
??????????????????????????????????????????????????????Model???????????Update-Database????????????????????
????????????????????????????????TableAttribute??ColumnAttribute????????????г????EF????TabelAttribute??ColumnAttribute?????????????????????????????в??????????????????????????Σ?????3?????????????????TableAttribute??ColumnAttribute?????????????Update-Database?????????????????????????????TableAttribute??????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11