· 8 years ago · Feb 10, 2018, 09:16 PM
1class Master
2{
3 public int Id { get; set; }
4 public string ToBeDeleted { get; set; }
5}
6
7class Detail
8{
9 public int Id { get; set; }
10
11 public Master Master { get; set; }
12}
13
14class Context : DbContext
15{
16 public DbSet<Master> Masters { get; set; }
17 public DbSet<Detail> Details { get; set; }
18
19 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
20 {
21 optionsBuilder.UseSqlite("Filename=local.db");
22 base.OnConfiguring(optionsBuilder);
23 }
24}
25
26class Program
27{
28 static void Main(string[] args)
29 {
30 using (var context = new Context())
31 {
32 context.Database.Migrate();
33 if(!context.Masters.Any())
34 {
35 var master = new Master {ToBeDeleted = "Some string"};
36 context.Add(master);
37 context.Add(new Detail {Master = master});
38 context.SaveChanges();
39 }
40 }
41 }
42}
43
44protected override void Up(MigrationBuilder migrationBuilder)
45{
46 migrationBuilder.DropColumn(
47 name: "ToBeDeleted",
48 table: "Masters");
49}
50
51protected override void Down(MigrationBuilder migrationBuilder)
52{
53 migrationBuilder.AddColumn<string>(
54 name: "ToBeDeleted",
55 table: "Masters",
56 nullable: true);
57}
58
59protected override void Up(MigrationBuilder migrationBuilder)
60{
61 migrationBuilder.Sql("PRAGMA foreign_keys=OFF");
62 migrationBuilder.CreateTable(
63 name: "NEW_Masters",
64 columns: table => new
65 {
66 Id = table.Column<int>(nullable: false)
67 .Annotation("Sqlite:Autoincrement", true),
68 },
69 constraints: table =>
70 {
71 table.PrimaryKey("PK_Masters", x => x.Id);
72 });
73 migrationBuilder.Sql("INSERT INTO NEW_Masters SELECT Id FROM Masters;");
74 migrationBuilder.DropTable("Masters");
75 migrationBuilder.RenameTable("NEW_Masters", newName: "Masters");
76 migrationBuilder.Sql("PRAGMA foreign_keys=OFF");
77}
78
79CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
80 "MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY,
81 "ProductVersion" TEXT NOT NULL
82);
83
84CREATE TABLE "Masters" (
85 "Id" INTEGER NOT NULL CONSTRAINT "PK_Masters" PRIMARY KEY AUTOINCREMENT,
86 "ToBeDeleted" TEXT
87);
88
89CREATE TABLE "Details" (
90 "Id" INTEGER NOT NULL CONSTRAINT "PK_Details" PRIMARY KEY AUTOINCREMENT,
91 "MasterId" INTEGER,
92 CONSTRAINT "FK_Details_Masters_MasterId" FOREIGN KEY ("MasterId") REFERENCES "Masters" ("Id") ON DELETE RESTRICT
93);
94
95CREATE INDEX "IX_Details_MasterId" ON "Details" ("MasterId");
96
97INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
98VALUES ('20170127204056_Migration1', '1.1.0-rtm-22752');
99
100INSERT INTO Masters (ToBeDeleted) VALUES ("ASDF"); --I've added this line manually for test only
101
102INSERT INTO Details (MasterId) VALUES (1); --I've added this line manually for test only
103
104PRAGMA foreign_keys="0";
105
106CREATE TABLE "NEW_Masters" (
107 "Id" INTEGER NOT NULL CONSTRAINT "PK_Masters" PRIMARY KEY AUTOINCREMENT
108);
109
110INSERT INTO NEW_Masters SELECT Id FROM Masters;;
111
112DROP TABLE "Masters";
113
114ALTER TABLE "NEW_Masters" RENAME TO "Masters";
115
116PRAGMA foreign_keys="1";
117
118INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
119VALUES ('20170127204851_Migration2', '1.1.0-rtm-22752');
120
121protected override void Up(MigrationBuilder migrationBuilder)
122 {
123 migrationBuilder.CreateTable(
124 name: "NEW_Masters",
125 columns: table => new
126 {
127 Id = table.Column<int>(nullable: false)
128 .Annotation("Sqlite:Autoincrement", true),
129 },
130 constraints: table =>
131 {
132 table.PrimaryKey("PK_Masters", x => x.Id);
133 });
134 migrationBuilder.Sql("INSERT INTO NEW_Masters SELECT Id FROM Masters;");
135 migrationBuilder.Sql("PRAGMA foreign_keys="0"", true);
136 migrationBuilder.Sql("DROP TABLE Masters", true);
137 migrationBuilder.Sql("ALTER TABLE NEW_Masters RENAME TO Masters", true);
138 migrationBuilder.Sql("PRAGMA foreign_keys="1"", true);
139 }
140
141//generated migration:
142
143 protected override void Up(MigrationBuilder migrationBuilder)
144 {
145 //comment out--
146 //migrationBuilder.DropColumn(
147 // name: "SocialMediaLinks",
148 // table: "ProfileUserVM");
149
150 //migrationBuilder.DropColumn(
151 // name: "SocialMediaLinks",
152 // table: "AspUsers");
153 }
154
155 protected override void Down(MigrationBuilder migrationBuilder)
156 {
157 migrationBuilder.AddColumn<string>(
158 name: "SocialMediaLinks",
159 table: "ProfileUserVM",
160 nullable: true);
161
162 migrationBuilder.AddColumn<string>(
163 name: "SocialMediaLinks",
164 table: "AspUsers",
165 nullable: true);
166 }