我已经搞砸了我的迁移,我用于IgnoreChanges初始迁移,但现在我想要删除所有迁移,并开始使用所有逻辑进行初始迁移.
当我删除文件夹中的迁移并尝试Add-Migration它并不生成一个完整的文件(它是空的 - 因为我上次没有做任何更改,但现在已删除,迁移).
是否有任何Disable-Migrations命令,所以我可以重新运行Enable-Migrations?
我试图首先使用Entity Framework代码来处理一个简单的数据库项目,然后遇到一个我根本想不通的问题.
我注意到EF正在为我的表设置ID,每次自动增加1,完全忽略我为该字段手动输入的值.经过一些搜索,我的理解是,正确的方法是禁用此行为:
modelBuilder.Entity<Event>().Property(e => e.EventID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Run Code Online (Sandbox Code Playgroud)
但是现在我只是得到了这个错误,我不明白为什么:
未处理的异常:System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误.有关详细信息,请参阅内部异常 ---
System.Data.UpdateException:更新条目时发生错误.有关详细信息,请参阅内部异常 ---> System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,无法在表'Events'中为identity列插入显式值.
如果它有用,这是有问题的POCO类:
public class Event
{
[Key, Required]
public int EventID { get; set; }
public string EventType { get; set; } //TODO: Event Type Table later on
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public virtual ICollection<Match> Matches { get; set; }
public virtual ICollection<EventParticipation> EventParticipation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
精简版:
删除整个数据库(包括__MigrationHistory)和解决方案中的所有迁移...不知何故,命名迁移正在某处找到并由DbMigrator应用!他们来自哪里???
长版:
我们正在使用Entity Framework 6,针对SQL Server 2012.它是一个MVC webapp,我们正在使用基于代码的迁移,此时我们有一个适度的迁移历史.我们在启动时运行迁移:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();
}
}
Run Code Online (Sandbox Code Playgroud)
在几台PC上它工作正常,但在一台新 PC上,我遇到了一些问题,它们在某些方面似乎不同步.每当应用程序运行时,它都会抱怨有待更改.
System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException occurred
HResult=-2146233088
Message=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)