如何设置自动递增属性初始值(DatabaseGeneratedOption.Identity) [英] How to set initial value for auto incremented property (DatabaseGeneratedOption.Identity)

查看:1303
本文介绍了如何设置自动递增属性初始值(DatabaseGeneratedOption.Identity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为优惠类如下:

public class Offer
{
    public Guid Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OfferNo { get; set; }

    public OfferType OfferType { get; set; }
    public DateTimeOffset DateAdded { get; private set; }
    public DateTimeOffset DateEdited { get; set; }
    public bool IsActive { get; set; }
}



我使用的标识属性作为我的PK明显。但我也需要显示报价的ID(因为用户会搜索使用该值报价)和GUID属性是太长时间。

I am using Id property as my PK obviously. But I also need to display the Id of offers (as users will search for offers using this value) and the Guid property is too long for that.

因此,我试图用DatabaseGeneratedOption.Identity自动增量整数列的 OfferNo ,但我不能设置一个初始值递增。我插入一个虚拟的项,然后试图将 OfferNo 以超过1人,但东西收到了以下异常:

Thus, I tried to use DatabaseGeneratedOption.Identity to auto increment the integer column OfferNo, but I can not set an initial value to increment on. I inserted a dummy entry and then tried to set OfferNo to something else than 1, but received the following exception:

修改不支持与身份的模式列。专栏:OfferNo。表:CodeFirstDatabaseSchema.Offer

我想设置使用代码首先这自动递增列的初始值。另一种解决方案也可以理解。

I would like to set the initial value of this auto incremented column using code-first. An alternative solution will also be appreciated.

推荐答案

根据该意见,但开始从种子值,我提供的,而不是1时,您可以使用标识列,和自定义数据库初始化或迁移来设置标识列的种子。

According to the comment, "but starting from a seed value that I provide instead of 1", you can use an identity column, and customize your database initialization or migration to set the seed of your identity column.

在T-SQL命令来做到这一点

The T-SQL command to do this is:

DBCC CHECKIDENT ('Offer', RESEED, 123);

注意 ,未来插入值不是123,而是123 +增量(124如果1缺省增量)。

您还可以使用与列在 DatabaseGeneratedOption.Computed 序列作为你的领域默认值(如果您使用的是最新的SQL Server版本)。当您创建一个序列,您可以指定初始值和增量:

You can also use a column with the DatabaseGeneratedOption.Computed and a sequence as default value for your field (if you're using a recent SQL server version). When you create a sequence, you can specify the initial value and increment:

CREATE SEQUENCE OfferNoSeq
START WITH 1 -- Initial Value
INCREMENT BY 1 -- Increment

这是附上该序列像这样的 OfferNo 列的默认:

An attach this sequence as a default for the OfferNo column like this:

ALTER TABLE Offer ADD CONSTRAINT OfferNoSeq 
DEFAULT (NEXT VALUE FOR OfferNoSeq)  FOR OfferNo;

有在代码中第一个实现这个没有直接的方法。因此,对于任何使用这些选项,你需要

There is no direct way to implement this in Code First. So, for using any of these options, you need to


  • customize数据库初始化这是通过实现自己的数据库初始化类完成和执行所需的SQL命令种子方法(寻找公共类MyInitializer 链接的文章中执行)

  • 或< A HREF =http://stackoverflow.com/questions/21731889/add-database-trigger-with-entity-framework-code-first-migrations>自定义迁移:您可以在执行任何SQL命令在向上()向下()迁移的方法,如所示链接的SO回答

  • customize the DB initialization This is done by implementing your own database initializer class and execute the desired SQL commands from the Seed methods (look for the implementation of public class MyInitializer in the linked article)
  • or to customize a migration: you can execute any SQL Command in the Up() or Down() method of your migration, as shown in the linked SO answer

这篇关于如何设置自动递增属性初始值(DatabaseGeneratedOption.Identity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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