在使用code first中,为什么数据库中已经有了一些表,但有时执行Add-Migration命令时,仍然需要新增已存在的表呢?如:
但是,执行
add-migration editdb -configurationtypename BJYLOnlineServ.Migrations.Administrative.Configuration
命令后,生成的DbMigration文件仍然提示再创建此表,如下:
CreateTable( "dbo.Adm_ApplyAttach", c => new { GId = c.Guid(nullable: false), Id = c.Decimal(nullable: false, precision: 18, scale: 0), FileName = c.String(maxLength: 64), FilePath = c.String(maxLength: 200), FileSize = c.Decimal(nullable: false, precision: 18, scale: 2), FileType = c.String(maxLength: 20, unicode: false), Namesigns = c.String(), SubmitFlag = c.Int(nullable: false), AdmApplyGId = c.Guid(nullable: false), AdmApplyId = c.Decimal(nullable: false, precision: 18, scale: 0), MaterialGId = c.Guid(nullable: false), Sys_CreatedDate = c.DateTime(nullable: false), Sys_UpdateDate = c.DateTime(nullable: false), Sys_CreatedUserId = c.String(), Sys_UpdatedUserId = c.String(), Sys_IsDeleted = c.Int(), License_GId = c.Guid(), }) .PrimaryKey(t => t.GId) .ForeignKey("dbo.Adm_Apply", t => t.AdmApplyGId, cascadeDelete: true) .ForeignKey("dbo.Adm_Materials", t => t.MaterialGId, cascadeDelete: true) .ForeignKey("dbo.License", t => t.License_GId) .Index(t => t.AdmApplyGId) .Index(t => t.MaterialGId) .Index(t => t.License_GId);
这到底是怎么回事啊,真是愁死我了!!!1
加上-IgnoreChanges参数试试
可是加上-IgnoreChanges,那我修改的Model信息也没有出来啊!
原本情况是:在Model中的添加了属性,然后再执行Add-Migration,目的是为了将添加的属性映射成字段添加到数据库中的。但是执行完后却让我重新创建表。
@一品邪女: editdb中的代码是什么?
@dudu: 执行完 add-migration editdb -configurationtypename BJYLOnlineServ.Migrations.Administrative.Configuration -IgnoreChanges -force
以后。editdb 就变成了:
namespace BJYLOnlineServ.Migrations.Administrative { using System; using System.Data.Entity.Migrations; public partial class editdb : DbMigration { public override void Up() { } public override void Down() { } } }
@一品邪女: 建议参考一下这篇博文:ASP.NET MVC4+EF4.1系列之三 Code First add-migration