EF6 Oracle代码首次迁移中的列的默认值 [英] EF6 Oracle default value for a column in code first migration

查看:350
本文介绍了EF6 Oracle代码首次迁移中的列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个迁移,使用 EntityFramework 6 为我的 Oracle 数据库中的NUMBER列设置默认值。这是我第一次尝试没有设置默认值:

I am trying to write a migration that sets Default Value for a NUMBER column in my Oracle database using EntityFramework 6. Here is my first try that didn't set the default value:

    public override void Up()
    {
        AddColumn("MTA.PLAN_SHEETS", "QUANTITY_CHANGED", c => c.Decimal(nullable: true, precision: 3, scale: 0, defaultValueSql: "1"));
    }

我还尝试使用 defaultValue 而不是 defaultValueSql ,而且还没有设置列的默认值。

我的迁移代码有什么问题吗? / p>

I also tried to use defaultValue instead of defaultValueSql and again it didn't set the default value for the column.
Is there anything wrong with my migration code?

推荐答案

我遇到与Oracle和EF6一样的问题。这将出现Oracle提供商不支持它。您有一个工作,如果你还没有找到一个。

I came across the same problem working with Oracle and EF6. It would appear the Oracle provider does not support it. There is a work around though, in case you haven't already found one.

您首先需要将QuantityChanged属性设置为可在您的模型中为空(或Fluent API,无论你在哪里处理这个)。然后,您可以运行add-migration命令,该命令将使用'Up'方法中的'AddColumn'方法生成一个迁移文件。之后,添加一个显式SQL命令将所有值更新为所需的默认值。如果您需要列为NOT NULL向前移动,您将需要另一个SQL命令来修改列并将其设置为NOT NULL。

You first need to set the QuantityChanged property as nullable in your model (or Fluent API, wherever you are handling this). Then you can run the add-migration command which will generate a migration file with the 'AddColumn' method in the 'Up' method. After that, add an explicit SQL command to update all values to the default value needed. If you need the column to be NOT NULL moving forward you will need another SQL command to modify the column and set it to NOT NULL.

    public override void Up()
    {
        AddColumn("MTA.PLAN_SHEETS", "QUANTITY_CHANGED", c => c.Decimal(precision: 3, scale: 0));
        Sql("UPDATE MTA.PLAN_SHEETS SET QUANTITY_CHANGED = 1");
        Sql("ALTER TABLE MTA.PLAN_SHEETS MODIFY QUANTITY_CHANGED NOT NULL");
    }

我希望这有帮助。参考我的问题,如果需要:如何使用EF6迁移设置新列的默认值?

I hope this helps. Reference my question if needed: How do I set a default value for a new column using EF6 migrations?

这篇关于EF6 Oracle代码首次迁移中的列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆