实体框架代码优先迁移失败,SQL Server

时间:2014-01-19 13:37:05

标签: c# entity-framework entity-framework-6

使用Entity Framework 6和SQL Server,当EF执行迁移时,我收到以下错误:

  

无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移。将挂起的模型更改写入基于代码的迁移或启用自动迁移。将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。

一切都与MySQL完美配合。

没有任何变化。运行另一个“添加 - 迁移”会导致空的Up()/ Down()方法:

public partial class Two : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

DbMigrationsConfiguration类:

internal sealed class Configuration : DbMigrationsConfiguration<MyDBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator("System.Data.SqlClient", new SqlServerMigrationSqlGenerator());
    }

    protected override void Seed(MyDBContext context)
    {
        ...
    }
}

DbContext.OnModelCreating

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());

        ...
    }

另外,检查__MigrationHistory似乎一切都井然有序。

我做错了什么?

更新: 暂时打开自动迁移以查看它所做的更改,它会将列精度/比例数从16/7更改为8/0。我不明白为什么它用一个精度创建它们然后希望它们稍后有一个不同的...但是...

更新2,查询:

以下是创建相关表格的查询:

CREATE TABLE [dbo].[Instruments] (
[ID] [int] NOT NULL IDENTITY,
[Symbol] [nvarchar](255),
[UnderlyingSymbol] [nvarchar](255),
[Name] [nvarchar](255),
[PrimaryExchangeID] [int],
[ExchangeID] [int],
[Type] [int] NOT NULL,
[Multiplier] [int],
[Expiration] [datetime],
[OptionType] [int],
[Strike] [decimal](16, 8),
[Currency] [nvarchar](25),
[MinTick] [decimal](16, 8),
[Industry] [nvarchar](255),
[Category] [nvarchar](255),
[Subcategory] [nvarchar](255),
[IsContinuousFuture] [bit] NOT NULL,
[ValidExchanges] [varchar](max),
[DatasourceID] [int] NOT NULL,
[ContinuousFutureID] [int],
[SessionsSource] [int] NOT NULL,
[SessionTemplateID] [int],
[DatasourceSymbol] [nvarchar](255),
CONSTRAINT [PK_dbo.Instruments] PRIMARY KEY ([ID])
)

CREATE TABLE [dbo].[exchangesessions] ( 
[ID] [int] NOT NULL IDENTITY,
[OpeningTime] [time](3) NOT NULL,
[ClosingTime] [time](3) NOT NULL,
[ExchangeID] [int] NOT NULL,
[IsSessionEnd] [bit] NOT NULL,
[OpeningDay] [int] NOT NULL,
[ClosingDay] [int] NOT NULL,
CONSTRAINT [PK_dbo.exchangesessions] PRIMARY KEY ([ID])
)

然后是自动迁移完成的查询:

ALTER TABLE [dbo].[Instruments] ALTER COLUMN [Expiration] [datetime]

ALTER TABLE [dbo].[exchangesessions] ALTER COLUMN [OpeningTime] [time](3) NOT NULL

ALTER TABLE [dbo].[exchangesessions] ALTER COLUMN [ClosingTime] [time](3) NOT NULL

1 个答案:

答案 0 :(得分:0)

一些想法: 日期精度 - .NET / C#具有DateTime,SQL服务器具有DateTime和DateTime2。添加约定

public class DateTime2Convention : Convention
{
    public DateTime2Convention()
    {
        this.Properties<DateTime>()
            .Configure(c => c.HasColumnType("datetime2"));
    }
}

然后添加

modelBuilder.Conventions.Add(new DateTime2Convention());

protected override void OnModelCreating(DbModelBuilder modelBuilder)

为Time创建一个类似的,应该解决这个问题。

另一件需要的是生成所需的SQL脚本。您可能需要从表__MigrationHistory中找到上次数据库更新的签名(MigrationID)并使用它来生成脚本

Update-Database -Script -SourceMigration:"201312141123260_MbrraceMigrations2" -TargetMigration:"201312191701134_MbrraceMigrations5"

或省略-Script以使用自动迁移。

对不起,这是一个不完整的答案,但我希望能帮助你。