简体   繁体   English

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

[英]Entity Framework Code-First Migration Fails with SQL Server

Using Entity Framework 6 and SQL Server, I get the following error when EF is doing the migration thing: 使用Entity Framework 6和SQL Server,当EF执行迁移操作时,出现以下错误:

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. DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。

Everything works perfectly fine with MySQL. 使用MySQL,一切正常。

There have been no changes. 没有变化。 Running another "Add-Migration" results in empty Up()/Down() methods: 运行另一个“ Add-Migration”将导致空的Up()/ Down()方法:

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

    public override void Down()
    {
    }
}

The DbMigrationsConfiguration class: DbMigrationsConfiguration类:

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

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

The DbContext.OnModelCreating : DbContext.OnModelCreating

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

        ...
    }

Also, examining the __MigrationHistory it seems like everything is in order. 另外,检查__MigrationHistory似乎一切都井井有条。

What am I doing wrong? 我究竟做错了什么?

An update: Turning on automatic migrations temporarily to see what changes it makes, it changes all the time column precision/scale numbers from 16/7 to 8/0. 一个更新:临时打开自动迁移以查看其所做的更改,它将所有时间列的精度/小数位数从16/7更改为8/0。 I don't understand why it creates them with one precision and then wants them to have a different one later on though... 我不明白为什么它会以一种精度创建它们,然后又希望它们以后又有不同的精度...

Update 2, the queries: 更新2,查询:

Here are the queries that create the relevant tables: 以下是创建相关表的查询:

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])
)

And then here are the queries done by the automatic migration: 然后是自动迁移完成的查询:

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

Some thoughts: Date-precision - .NET/C# has DateTime, SQL server has DateTime and DateTime2. 一些想法:日期精度-.NET / C#具有DateTime,SQL Server具有DateTime和DateTime2。 Add the convention 添加约定

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

then add 然后加

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

to

protected override void OnModelCreating(DbModelBuilder modelBuilder)

Create a similar one for Time and that should address that problem. 为“时间”创建一个类似的名称,这应该可以解决该问题。

The other thing that is required is to generate the SQL script required. 所需的另一件事是生成所需的SQL脚本。 You can need to find the signature (MigrationID) of the last database update from the table __MigrationHistory and use that to either generate a script using 您可能需要从表__MigrationHistory上次数据库更新的签名(MigrationID),并使用该签名来生成脚本,该脚本使用

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

or omit -Script to use automatic migrations. 或省略-Script以使用自动迁移。

Sorry it's a bit of a patchy answer but I hope that will get you going. 抱歉,这个答案有点不完整,但我希望这能帮助您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 使用代码优先的迁移和实体框架 - Going with code-first migration and entity framework 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration 实体框架核心multipleDbContext代码优先迁移共享实体 - Entity framework core multipleDbContext code-first migration of shared entity 使用实体框架在代码优先迁移中有条件地插入数据 - Conditionally inserting data in a code-first migration with Entity Framework Entity Framework Core错误中的代码优先迁移 - Code-first migration in Entity Framework Core error 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship 使用代码优先迁移的Entity Framework从SQL Server到Oracle - From SQL Server to Oracle using Entity Framework with code-first Migrations SQL Server表设计建议(Entity Framework代码优先,C#) - SQL Server table design advice (Entity Framework code-first, C#) 实体框架代码优先定位LocalDb而不是本地SQL Server Express主机 - Entity Framework code-first targeting LocalDb instead of local SQL Server Express host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM