如何将Identity属性修改为True [英] How to Modify the Identity Property to True

查看:493
本文介绍了如何将Identity属性修改为True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在管理一个首先使用代码来创建其数据库的Web应用程序。其中一个数据库表当前将其
Identity 属性设置为 False ,即使此表的QA实例另有说明。因此,我有两个问题:



  1. 我想将属性更改为 True 但是因为应用程序首先使用代码,如果我通过应用程序更改它,或者我仍然可以执行http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column
    中的说明无论应用程序是否首先使用代码?



  2. 在此表的配置类中,设置了以下配置。由于
    AutomaticMigrationsEnabled 属性设置为 True ,为什么此表的Identity属性为False?



  3. <预类= "prettyprint">

    公共ABCConfiguration()&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;

    {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    ToTable(QUOT; ABC");&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    HasKey(X => x.Id);&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    属性(X => x.Id)&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;

    .HasColumnName(QUOT;标识")&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;

    .HasColumnType(SqlDbType.Int.ToString())&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;

    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    / *其他配置... * /

    }

    技术:



    • Visual Studio 2013 (C#.NET)
    • 实体框架6.0
    • 代码优先
    • SQL Server 2012

    解决方案

    这是一个SQL Server论坛,我们对"代码优先"一无所知。这里。我可以说的是,添加/删除列的IDENTITY属性很痛苦,因为您需要重建表。另一方面,如果您使用序列,
    只需添加默认值"NEXT VALUE FOR YourSequence"即可。到柱上。


    <名称= "x_x_e22c28b0-76a9-4665-86cb-7d1f9fc0f6d4_CommunityBridge" 标题="Xnews / 2005年10月3日的Mime代理/ 1.4.c.4的(Win32)通过社区论坛NNTP Server 1.1.55.0">


    I am managing a web application that is using code first to create its database. One of the database tables currently has its Identity property set to False even though the QA instance of this table shows otherwise. Therefore, I have two questions:

    1. I want to change the property to True but since the application is using code first, should I change it via the application or can I still perform the instructions in http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column regardless if the application is using code first or not?

    2. In the configuration class of this table, the following configuration is set. Since the AutomaticMigrationsEnabled property is set to True, why then is this table's Identity property False?

    public ABCConfiguration()               

    {               

    ToTable("ABC");               

    HasKey(x => x.Id);               

    Property(x => x.Id)                   

    .HasColumnName("Id")                   

    .HasColumnType(SqlDbType.Int.ToString())                   

    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

    /*additional configurations...*/

    }

    Technologies:

    • Visual Studio 2013 (C#.NET)
    • Entity Framework 6.0
    • Code First
    • SQL Server 2012

    解决方案

    This is an SQL Server forum, and we know nothing about "code first" here. What I can say is that adding/removing the IDENTITY property of a column is painful, because you need to rebuild the table. On the other hand, if you use a sequence instead, you can simply add a default value of "NEXT VALUE FOR YourSequence" to the column.


    这篇关于如何将Identity属性修改为True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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