{"rsdb":{"rid":"239372","subhead":"","postdate":"0","aid":"164987","fid":"106","uid":"1","topic":"1","content":"
<\/p> \n
\u5229\u7528Code First\u6a21\u5f0f\u6784\u5efa\u597d\u57fa\u672c\u7684\u7c7b\u540e\uff0c\u9879\u76ee\u4e5f\u5f00\u59cb\u642d\u5efa\u5b8c\u6bd5\u5e76\u6210\u529f\u8fd0\u884c\uff0c\u800c\u4e14\u5df2\u7ecf\u5c06\u6570\u636e\u5e93\u8868\u7ed3\u6784\u81ea\u52a8\u751f\u6210\u4e86\u3002<\/p> \n
\u4f46\u662f,\u6211\u6709\u65b0\u7684\u7c7b\u8981\u52a0\u5165,\u6709\u5b57\u6bb5\u9700\u8981\u4fee\u6539\uff0c\u90a3\u600e\u4e48\u529e\u5462\uff0c\u5220\u5e93\uff0c\u8dd1\u8def \uff1f \u54c8\u54c8 <\/p> \n
<\/p> \n
\u5229\u7528\u6570\u636e\u5e93\u8fc1\u79fb\uff0c\u5c06\u539f\u6709\u7ed3\u6784\u4e0d\u6539\u52a8\uff0c\u5c06\u65b0\u5efa\u7c7b\u8fdb\u884c\u5355\u72ec\u5efa\u8868\u64cd\u4f5c\uff0c\u6216\u8005\u662f\u5df2\u6709\u6570\u636e\u5e93\u8868\uff0c\u6539\u53d8\u5b57\u6bb5\uff0c\u90a3\u5c31\u4fee\u6539\u8868\u3002<\/p> \n
<\/p> \n
1.\u6253\u5f00\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0<\/strong><\/strong>\uff1a\u5de5\u5177->NuGet\u5305\u7ba1\u7406\u5668->\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0.(\u5f53\u7136\u8fd8\u6709\u5176\u5b83\u65b9\u5f0f\u4e5f\u53ef\u4ee5\u6253\u5f00\uff0c\u6211\u6bd4\u8f83\u559c\u6b22\u8fd9\u79cd)<\/strong><\/span><\/strong><\/p> \n <\/p> \n \u70b9\u51fb\u540e\u5c06\u5f39\u51fa\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0<\/span><\/p> \n <\/p> \n \u6781\u5176\u8981\u6ce8\u610f\u7684\u662f\u9ed8\u8ba4\u9879\u76ee\uff01\uff01\uff01<\/span><\/p> \n 2.\u542f\u52a8\u6570\u636e\u5e93\u8fc1\u79fb\uff0c\u6267\u884c\u547d\u4ee4\uff1aenable-migrations<\/strong><\/p> \n <\/strong><\/p> \n <\/p> \n \u7531\u4e8eModels\u4e0d\u4e00\u5b9a\u5c31\u662f\u76f4\u63a5\u5229\u7528\u9879\u76ee\u4e2d\u5df2\u6709\u7684\u6587\u4ef6\u5939Models,\u4ece\u800c\u5f53\u4f60\u9009\u4e2d\u542f\u52a8\u9879\u76ee\u4e3aasp.net mvc \u6846\u67b6\u7c7b\u578b\u65f6\uff0c\u6b64\u5904\u5c06\u4f1a\u62a5\u9519<\/p> \n \u5177\u4f53\u6765\u770b\u6211\u5f53\u524d\u9879\u76ee\u7684\u5206\u5e03\u60c5\u51b5\uff1a<\/p> \n <\/p> \n \u6211\u5c06\u6a21\u578b\u5206\u79bb\u51fa\u6765\u4e86\uff0c\u4ee5\u81f3\u4e8e\u5f53\u6211\u9ed8\u8ba4\u9009\u4e2dSearchEngine\u4e3a\u542f\u52a8\u9879\u76ee\u65f6\uff0c\u5728\u7a0b\u5e8f\u5305\u7ba1\u7406\u5668\u63a7\u5236\u53f0<\/span>\u4e2d\u8f93\u5165\u7684\u7b2c\u4e00\u4e2a\u547d\u4ee4\u5c31\u4f1a\u62a5\u9519<\/p> \n <\/p> \n <\/p> \n \u89e3\u51b3\u65b9\u6848\uff1a\u9009\u4e2d\u9ed8\u8ba4\u9879\u76ee\uff0c\u9009\u62e9\u4e3aDbContext\u6240\u5728\u7684\u9879\u76ee\uff0c\u6b64\u5904\u6211\u7684\u662f\u7c7b\u5e93<\/span><\/p> \n \u6210\u529f\u64cd\u4f5c\u4f1a\u5982\u4e0b\u63d0\u793a\uff1a<\/p> \n <\/p> \n \u5e76\u4e14\u5728DbContext\u540c\u7ea7\u76ee\u5f55\u4e0b\u589e\u52a0\u4e86migrations\u76ee\u5f55<\/p> \n <\/p> \n <\/p> \n 3.\u5f00\u59cb\u6570\u636e\u5e93\u8fc1\u79fb\u547d\u4ee4<\/strong><\/strong><\/p> \n 1.Add-migration [\u81ea\u5b9a\u4e49\u7248\u672c\u540d\u79f0]<\/strong><\/strong><\/p> \n \u3000\u3000\u8f93\u5165add-migration updateorderheader<\/strong><\/strong><\/strong><\/strong><\/p> \n <\/strong><\/strong><\/strong><\/p> \n 2.update-database<\/strong><\/p> \n \u3000\u3000\u8f93\u5165update-database<\/strong><\/strong><\/strong><\/strong><\/p> \n <\/p> \n <\/p> \n \u6b64\u5904\u5982\u679c\u548c\u6211\u4e00\u6837\u5c06DbContext\u5355\u72ec\u5206\u5272\u51fa\u6765\u7684\u9700\u8981\u6ce8\u610f\uff0c\u9700\u8981\u5728\u5f53\u524d\u7c7b\u5e93\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u4e0a\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5730\u5740<\/p> \n <\/p> \n <\/p> \n \u5982\u679c\u662f\u76f4\u63a5\u5728asp.net mvc\u6846\u67b6\u4e2d\u7684Models\u4e2d\u52a0\u5165\u7684DbContext\uff0c\u5219\u53ef\u4e0d\u5fc5\uff0c\u4f1a\u76f4\u63a5\u8bfb\u53d6Web.Config\u6587\u4ef6\u4e2d\u7684\u8fde\u63a5\u5b57\u7b26\u4e32<\/span><\/p> \n <\/p> \n \u7b49\u5f85\u547d\u4ee4\u6267\u884c\u4e2d......<\/p> \n \u51fa\u73b0\u5f02\u5e38\u4e86<\/p> \n <\/p> \n <\/p> \n \u5f00\u59cb\u5bfb\u627e\u6211\u7535\u8111\u4e2d\u7684SQL Server Configuration Manage \uff0c\u5982\u679c\u6709\u5219\u6253\u5f00\uff0c\u627e\u5230<\/p> \n <\/p> \n <\/p> \n \u914d\u7f6eTCP\/IP\u534f\u8bae\u4e3a\u5df2\u542f\u7528\u5373\u53ef\u3002<\/p> \n \u6211\u7684\u672c\u5730\u6ca1\u6709\u8be5\u5de5\u5177\uff0c\u53ea\u80fd\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u627e\u5230\u8be5\u5904<\/p> \n \u70b9\u51fb\u6211\u7684\u7535\u8111\u53f3\u952e->\u7ba1\u7406->\u670d\u52a1\u548c\u5e94\u7528\u7a0b\u5e8f->SQLServer\u914d\u7f6e\u7ba1\u7406\u5668 ( \u4e24\u6b21\u627e\u8fd9\u5de5\u5177\u90fd\u6ca1\u627e\u5230\uff0c\u8c8c\u4f3c\u90fd\u662f\u76f4\u63a5\u4ece\u8fd9\u89e3\u51b3\u7684\uff09<\/p> \n \u91cd\u65b0\u8f93\u5165\u547d\u4ee4\uff0c\u7b49\u5f85\u6267\u884c<\/p> \n <\/p> \n \u521d\u6b65\u6210\u529f<\/p> \n <\/p> \n \u7531\u4e8e\u6570\u636e\u5e93\u4e2d\u6709\u4e9b\u8868\u65e0\u9700\u66f4\u6539<\/p> \n \u6211\u4eec\u53ef\u4ee5\u5728migrations\u76ee\u5f55\u4e2d\uff0c\u66f4\u6539\u9700\u8981\u589e\u52a0\u6216\u4fee\u6539\u7684\u5177\u4f53\u8868<\/p> \n <\/p> \n <\/p> \n <\/p> \n \u901a\u8fc7\u4fee\u6539\u5176\u4e2d\u7684CreateTable\/DropTable\uff0c\u53ef\u4ee5\u63a7\u5236\u54ea\u4e9b\u8868\u9700\u8981\u6539\u52a8<\/p> \n \u4fee\u6539migrations\u76ee\u5f55\u4e2d\u7684Configuration.cs\u6587\u4ef6\uff0c\u5f00\u542f\u81ea\u52a8\u8fc1\u79fb<\/strong><\/p> \n <\/strong><\/p> \n 5.\u5728\u9879\u76eeGlobal.asax\u7684Application_Start\u4e2d\u52a0\u4e0a\u5982\u4e0b\u4ee3\u7801\u884c\uff1a<\/strong><\/p> \n <\/p> \n <\/p> \n \u5230\u6b64\uff0c\u6570\u636e\u5e93\u8fc1\u79fb\u5de5\u4f5c\u5df2\u7ecf\u5b8c\u6bd5\u4e86\u3002<\/strong><\/p> \n <\/p> \n <\/p>\n<\/div>","orderid":"0","title":"asp.net mvc CodeFirst\u6a21\u5f0f\u6570\u636e\u5e93\u8fc1\u79fb\u6b65\u9aa4","smalltitle":"","mid":"0","fname":".NET","special_id":"0","bak_id":"0","info":"0","hits":"9906","pages":"1","comments":"0","posttime":"2017-10-16 18:19:49","list":"1508149189","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/images2017.cnblogs.com\/blog\/1133736\/201710\/1133736-20171015114531902-1750849984.png","ispic":"1","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"asp.net<\/A> mvc<\/A> CodeFirst<\/A> \u6a21\u5f0f<\/A> \u6570\u636e\u5e93<\/A> \u8fc1\u79fb<\/A> \u6b65\u9aa4<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"asp.net mvc CodeFirst\u6a21\u5f0f\u6570\u636e\u5e93\u8fc1\u79fb\u6b65\u9aa4","lastview":"1713984268","digg_num":"7840","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}namespace<\/span> SAssassin.EF.Model.Migrations\n{\n <\/span>using<\/span> System;\n <\/span>using<\/span> System.Data.Entity.Migrations;\n\n <\/span>public<\/span> partial<\/span> class<\/span> updateorderheader : DbMigration\n {\n <\/span>public<\/span> override<\/span> void<\/span> Up()\n {\n CreateTable(\n <\/span>"<\/span>dbo.MyFileInfoes<\/span>"<\/span>,\n c <\/span>=> new<\/span>\n {\n Id <\/span>= c.Int(nullable: false<\/span>, identity: true<\/span>),\n FileName <\/span>= c.String(),\n FileDescription <\/span>= c.String(),\n FilePath <\/span>= c.String(),\n FileType <\/span>= c.Byte(nullable: false<\/span>),\n FileSize <\/span>= c.String(),\n FileStatus <\/span>= c.Byte(nullable: false<\/span>),\n IsPublic <\/span>= c.Byte(nullable: false<\/span>),\n UserId <\/span>= c.String(),\n UserName <\/span>= c.String(),\n CreateDate <\/span>= c.DateTime(nullable: false<\/span>),\n LastModityDate <\/span>= c.DateTime(nullable: false<\/span>),\n })\n .PrimaryKey(t <\/span>=> t.Id);\n }\n\n <\/span>public<\/span> override<\/span> void<\/span> Down()\n {\n DropTable(<\/span>"<\/span>dbo.MyFileInfoes<\/span>"<\/span>);\n }\n }\n}<\/span><\/pre> \n <\/div> \n
4.\u4fee\u6539\u4ee3\u7801\u914d\u7f6e\uff0c\u5b9e\u73b0\u4e4b\u540e\u7684\u64cd\u4f5c\u6ca1\u90a3\u4e48\u9ebb\u70e6<\/strong><\/p> \n System.Data.Entity.Database.SetInitializer(new<\/span> System.Data.Entity.MigrateDatabaseToLatestVersion<SAssassin.EF.Model.CodeFirst, SAssassin.EF.Model.Migrations.Configuration>());<\/pre> \n <\/div> \n
2017-10-15,\u671b\u6280\u672f\u6709\u6210\u540e\u80fd\u56de\u6765\u770b\u89c1\u81ea\u5df1\u7684\u811a\u6b65\u3002<\/strong><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/code><\/pre> \n