这看起来像一个非常常见的任务,但我找不到一个简单的方法来做到这一点.
我想撤消上次应用的迁移.我本来期待一个简单的命令,比如
PM> Update-Database -TargetMigration:"-1"
Run Code Online (Sandbox Code Playgroud)
相反,我能想出的就是:
PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
201208012131302_Add-SystemCategory
201207311827468_CategoryIdIsLong
201207232247409_AutomaticMigration
201207211340509_AutomaticMigration
201207200025294_InitialCreate
PM> Update-Database -TargetMigration:"CategoryIdIsLong"
Run Code Online (Sandbox Code Playgroud)
(至少我可以只使用名称,跳过时间戳......)
有没有更简单的方法?
entity-framework database-migration entity-framework-4 ef-migrations
我使用迁移(EF 5.0)和代码优先有一个有趣的效果:
我用GUID主键创建了一些模型.(顺便说一句:对我来说,SQL Server使用它很重要NEWSEQUENTIALID(),这似乎是当前版本中的默认值)
在某些时候,我激活了迁移.我在初始迁移中添加了一些代码,这主要是.Index()根据需要.
当我删除数据库并调用update-database时,出现以下错误:
无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移.您可以使用Add-Migration命令将挂起的模型更改写入基于代码的迁移.
我尝试过AutomaticMigrationsEnabled = true,无需更改或添加任何内容即可使用!
但既然我不想要AutomaticMigrationsEnabled,我也尝试再次删除数据库,update-database然后调用add-migration.我最终得到了一个似乎没有改变任何东西的额外迁移(见下文).我也尝试将这些行添加到初始迁移的底部 - 但这并没有改变任何东西.
其中一个型号:
[Table(Speaker.TABLENAME)]
public class Speaker : BaseModel
{
public const String TABLENAME = "Speaker";
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[Required]
[MaxLength(50, ErrorMessage = "Name must be 50 characters or less")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
初始迁移代码:
public partial class InitialCreate : DbMigration
{
public override void Up()
{ …Run Code Online (Sandbox Code Playgroud) 有没有办法在Entity Framework 4.3.1中禁用迁移?我从项目和我的数据库中生成的表中删除了迁移文件夹,但它不起作用!你怎么能删除迁移?
我的MVC应用程序中有两个实体,我使用Entity Framework 6 Code First方法填充数据库.学生实体中有两个城市ID; 其中一个为BirthCity,另一个为WorkingCity.当我在上面定义外键时,在迁移后在Student表中创建一个名为City_ID的额外列.我有错误或如何定义这些FK?提前致谢.
学生:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
市:
public class City
{
public int ID { get; set; }
public string CityName …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework foreign-keys foreign-key-relationship ef-code-first
在我从同一教程创建的VS2015项目中运行该命令后,遵循此MicrosoftPM> Add-Migration MyFirstMigration教程我得到以下错误,我无法解决:
More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.
Run Code Online (Sandbox Code Playgroud)
要点注意
Individual User Account身份验证而不是No Authentication在教程中使用.ASP.NeT Core 1.0 and VS2015-Update 3 on windows 8.1我正在尝试让Entity框架迁移工作.我已启用代码首次迁移,它创建了迁移文件夹,配置文件和迁移历史记录表,但没有初始创建.我错过了一步吗?这是由EF(4.3.1)创建的新数据库.
在我的MVC应用程序中,我使用了Entity Framework 6并使用代码优先方法创建了数据库.经过一段时间后,我通过添加新列并删除了一些列来更新其中一个实体类.为了将这些更改反映到数据库,我按照以下步骤操作:
- 删除了项目中的迁移文件夹.
- 删除了数据库中的__MigrationHistory表.
然后在程序包管理器控制台中运行以下命令:
Enable-Migrations -EnableAutomaticMigrations -Force在配置文件中添加以下行:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;运行:
Add-Migration Initial- 最后,运行:
Update-Database -Verbose
但是,我遇到一个错误"数据库中已经有一个名为'xxx'的对象."
为了摆脱这个问题,我在第5步之后创建的初始文件中注释Up方法中的代码.这可以防止错误,但数据库中没有任何更改(更新的实体表保持不变).哪里出错了?在此先感谢您的帮助.
这是我在migration.cs文件中注释的Up方法:
public override void Up()
{
CreateTable(
"dbo.City",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
RegionID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Region", t => t.RegionID)
.Index(t => t.RegionID);
CreateTable(
"dbo.Multiplier",
c => new
{
ID = c.Int(nullable: false, identity: true),
Status = c.Int(nullable: false),
Term = …Run Code Online (Sandbox Code Playgroud) sql-server asp.net-mvc entity-framework updatemodel ef-code-first
我之前使用授权时遇到了一些问题,因此我获得了全新的一切 - 新的计算机,新操作系统,VS的全新安装,新应用程序和Azure上的新资源组中的数据库.整个shabang.
我可以确认我可以登录到Azure DB,如下面的屏幕截图所示.
我可以看到数据库,表格,用户等.
问题是,虽然它在本地工作(使用我自动提供的默认连接字符串),但它在Azure中表现不佳(尽管我从那里使用发布文件).它说了一些关于文件未找到的内容,根据这个答案,我需要更改连接字符串.
我改变后,我得到以下错误.请注意防火墙已打开,我可以在运行应用程序代码时访问数据库.我觉得在自动配置身份验证部分时会出现问题.不过,我对如何解决问题没有想法.
[SqlException(0x80131904):用户'Chamster'登录失败.已为此会话分配了跟踪ID"09121235-87f3-4a92-a371-50bc475306ca".在需要帮助时,请将此跟踪ID提供给客户支持.]
我正在使用的连接字符串是这个.
Server=tcp:f8goq0bvq7.database.windows.net,1433;
Database=Squicker;
User ID=Chamster@f8goq0bvq7;
Password=Abc123();
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=10;
Run Code Online (Sandbox Code Playgroud)
这个问题困扰了我一段时间,我会在两天内把它给它.任何建议都热烈赞赏.
我一直在项目中使用Entity Framework(5.0)一段时间(VS2012 Express中的ASP.NET MVC).但是,现在,我无法再添加迁移.
PM > Add-Migration -projectName MyProject.DAL TestMigration
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
Run Code Online (Sandbox Code Playgroud)
我不知道这是否提供任何线索,但"无法..."文本显示为红色.
我试图启用自动迁移(这是没有意义的,因为我正在尝试将挂起的模型更改写入基于代码的迁移)并导致数据库中所需的迁移.然而,这不是我想要的,因为我在项目中没有迁移.
我试图删除数据库并重新创建数据库.重新创建数据库(直到上一次迁移),但是当我尝试使用Add-Migration时,仍然会出现"无法更新.."错误.
编辑
我尝试了-force参数,但没有区别.
我的配置类的内容(我在上一次迁移后没有改变任何内容):
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
{
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate);
context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
}
Run Code Online (Sandbox Code Playgroud)
编辑2 我发现当我在DbContext中评论以下行时,我能够进行添加迁移:
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, …Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework 7 beta 5.我开始使用带有用户管理的mvc模板.在我的试验期间,我添加了几次迁移.
现在我想删除我的数据库,创建一个新的初始迁移并用它生成一个新的干净数据库.
但是,dnx ef命令没有我知道的命令,这将有助于此.我该怎么办?
asp.net-core ×2
asp.net-mvc ×2
c# ×2
sql-server ×2
azure ×1
code-first ×1
foreign-keys ×1
migration ×1
updatemodel ×1