首先用code禁用标识(自动递增)整数主键 [英] Disabling identity (auto-incrementing) on integer primary key using code first

查看:97
本文介绍了首先用code禁用标识(自动递增)整数主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用code第一种方法在ASP.NET MVC 3应用程序,并在模型中的所有整数主键(公众诠释标识{获取;集;} )默认配置为自动递增的身份。如何禁用该功能,使的方式来手动输入主键整数?

I am using code first approach in a ASP.NET MVC 3 application and all integer primary keys in models (public int Id { get; set; }) are by default configured as an identity with auto-incrementing. How to disable this and enable a way to manually enter the integer for the primary key?

而实际情况是,编号整数具有特殊的意义,所以我想有他们在创建可选择的,后来编辑。如果万一整数不是在创建时给出它是自动递增,否则用于规定值那将是理想的。但是,编辑主要领域是我的第一需要。有没有办法做到这一点优雅的ASP.NET MVC 3?

The actual situation is that the Id integers have a special meaning and I would therefore like to have them choosable at creation and later editable. It would be ideal if in case the integer is not given at creation time it is auto-incremented, else the specified value is used. But editable primary fields is my primary need. Is there any way to do this elegantly in ASP.NET MVC 3?

推荐答案

使用这些数据的注释选项:

Use these data annotation options:


  • [System.ComponentModel.DataAnnotations.KeyAttribute()]

  • <$c$c>[System.ComponentModel.DataAnnotations.DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]

  • [System.ComponentModel.DataAnnotations.KeyAttribute()]
  • [System.ComponentModel.DataAnnotations.DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]

这篇关于首先用code禁用标识(自动递增)整数主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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