aps.net Mvc中生成动态数据库,大家需要注意的是每个表的主键最好表名_ID,这样比较明确。
首先需要创建模型(model)层,需要的属性
1 public class User 2 { 3 /// <summary> 4 /// 编号 5 /// </summary> 6 [Key] 7 public int uID { get; set; } 8 9 /// <summary> 10 /// 姓名 11 /// </summary> 12 public string Name { get; set; } 13 14 /// <summary> 15 /// 昵称 16 /// </summary> 17 public string NickName { get; set; } 18 19 /// <summary> 20 /// 性别 0:男 1:女 21 /// </summary> 22 public int Sex { get; set; } 23 24 /// <summary> 25 /// 年龄 26 /// </summary> 27 public int Age { get; set; } 28 29 /// <summary> 30 /// 出生日期 31 /// </summary> 32 public DateTime Birthday { get; set; } 33 34 /// <summary> 35 /// 地址 36 /// </summary> 37 public string Address { get; set; } 38 39 /// <summary> 40 /// 职业ID 41 /// </summary> 42 public int OccID { get; set; } 43 44 /// <summary> 45 /// 省ID 46 /// </summary> 47 public int PID { get; set; } 48 49 /// <summary> 50 /// 市ID 51 /// </summary> 52 public int CID { get; set; } 53 54 /// <summary> 55 /// 区ID 56 /// </summary> 57 public int AID { get; set; } 58 59 }
然后,创建一个模拟的数据库,需要我们从EF的DbContext类中派生出一个类来访问数据库:
1 public class DataContext : DbContext 2 { 3 4 #region 构造函数 5 public DataContext() 6 { 7 Configuration.LazyLoadingEnabled = true; 8 } 9 #endregion 10 11 12 public DbSet<User> Users { get; set; } 13 14 public DbSet<Occupation> Occupations { get; set; } 15 16 public DbSet<Province> Provinces { get; set; } 17 18 public DbSet<City> Cities { get; set; } 19 20 public DbSet<Area> Areas { get; set; } 21 22 23 }
但是,我们创建的好的数据库和表,有时候我们会修改表中的字段,就会出现"支持“DataContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。"这个问题
在vs2012 工具 菜单里,点击“库程序包管理器”-》程序包管理器控制台 在控制台的PM>标记后,输入“Enable-Migrations -ContextTypeName MvcApplication.Migrations.DataContext”
显示 已为项目 MvcApplication 启用 Code First 迁移。
上面的Enable-Migrations命令创建了一个新的Migrations文件夹,并在该目录下创建了Configuration.cs文件。 使用Visual Studio打开Configuration.cs文件。
1 internal sealed class Configuration : DbMigrationsConfiguration<DataContext> 2 { 3 public Configuration() 4 { 5 AutomaticMigrationsEnabled = false; 6 } 7 8 protected override void Seed(DataContext context) 9 { 10 // This method will be called after migrating to the latest version. 11 12 // You can use the DbSet<T>.AddOrUpdate() helper extension method 13 // to avoid creating duplicate seed data. E.g. 14 // 15 // context.People.AddOrUpdate( 16 // p => p.FullName, 17 // new Person { FullName = "Andrew Peters" }, 18 // new Person { FullName = "Brice Lambson" }, 19 // new Person { FullName = "Rowan Miller" } 20 // ); 21 // 22 } 23 } 24 }
下一步是创建DbMigration 类来初始化迁移。迁移将创建一个新的数据库,这也是在前面步骤中删除数据库文件的原因。 在程序包管理器控制台窗口,输入命令"add-migration Initial"来创建初始迁移。其中Initial可以是任意名称,用来标识创建的初始文件。控制台输出如下:
PM> add-migration InitialData 正在为迁移“InitialData”搭建基架。此迁移文件的设计器代码包含当前 Code First 模型的快照。在下一次搭建迁移基架时,将使用此快照计算对模型的更改。如果对要包含在此迁移中的模型进行其他更改,则您可通过再次运行“Add-Migration InitialData”重新搭建基架。
代码先行迁移机制在Migrations文件夹下创建另外一个类文件,文件名为时间戳+下划线+ InitialData.cs,例如201311040832031_InitialData.cs,
1 public partial class InitialData : DbMigration 2 { 3 public override void Up() 4 { 5 CreateTable( 6 "dbo.Areas", 7 c => new 8 { 9 areaID = c.Int(nullable: false, identity: true), 10 Name = c.String(), 11 PID = c.Int(nullable: false), 12 CID = c.Int(nullable: false), 13 }) 14 .PrimaryKey(t => t.areaID); 15 16 CreateTable( 17 "dbo.Cities", 18 c => new 19 { 20 cityID = c.Int(nullable: false, identity: true), 21 Name = c.String(), 22 PID = c.Int(nullable: false), 23 }) 24 .PrimaryKey(t => t.cityID); 25 26 CreateTable( 27 "dbo.Occupations", 28 c => new 29 { 30 occID = c.Int(nullable: false, identity: true), 31 OccName = c.String(), 32 }) 33 .PrimaryKey(t => t.occID); 34 35 CreateTable( 36 "dbo.Provinces", 37 c => new 38 { 39 proID = c.Int(nullable: false, identity: true), 40 Name = c.String(), 41 }) 42 .PrimaryKey(t => t.proID); 43 44 CreateTable( 45 "dbo.Users", 46 c => new 47 { 48 uID = c.Int(nullable: false, identity: true), 49 Name = c.String(), 50 NickName = c.String(), 51 Sex = c.Int(nullable: false), 52 Age = c.Int(nullable: false), 53 Birthday = c.DateTime(nullable: false), 54 Address = c.String(), 55 OccID = c.Int(nullable: false), 56 PID = c.Int(nullable: false), 57 CID = c.Int(nullable: false), 58 AID = c.Int(nullable: false), 59 }) 60 .PrimaryKey(t => t.uID); 61 62 } 63 64 public override void Down() 65 { 66 DropTable("dbo.Users"); 67 DropTable("dbo.Provinces"); 68 DropTable("dbo.Occupations"); 69 DropTable("dbo.Cities"); 70 DropTable("dbo.Areas"); 71 } 72 }
该类包含了创建数据库架构的代码。迁移文件名预置为时间戳有助于排序。查看该文件,包含了创建Movie库表的说明。当你更新数据库时,该类将被执行,创建数据库架构。随后,Seed方法将被执行,测试数据被添加其中。
在程序包管理器控制台窗口,键入"update-database"命令来创建数据库和执行Seed方法。
PM> update-database 指定“-Verbose”标志以查看应用于目标数据库的 SQL 语句。 正在应用显式迁移: [201311040832031_InitialData]。 正在应用显式迁移: 201311040832031_InitialData。 正在运行 Seed 方法。
PM>
如果遇到表已存在的错误而不能创建,很可能是你删除数据库后,执行update-database前运行了应用程序(重新编译程序,自动创建了数据库)。这种情况下,再次删除数据库文件,并执行 update-database命令。如果仍然出错,删除migrations目录和内容,重新开始本教程操作。